blob: 0d36c4fd4684278f4333f5d51d08a037476378ad [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
Andrew Trickfe05d982012-10-03 23:06:25 +000014#define DEBUG_TYPE "subtarget-emitter"
15
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000016#include "CodeGenTarget.h"
Andrew Trick2661b412012-07-07 04:00:00 +000017#include "CodeGenSchedule.h"
Andrew Trick40096d22012-09-17 22:18:45 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000019#include "llvm/ADT/StringExtras.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000020#include "llvm/MC/MCInstrItineraries.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/Format.h"
Andrew Trick40096d22012-09-17 22:18:45 +000023#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000024#include "llvm/TableGen/Record.h"
25#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohen9489c042005-10-28 01:43:09 +000026#include <algorithm>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000027#include <map>
28#include <string>
29#include <vector>
Jim Laskey4bb9cbb2005-10-21 19:00:04 +000030using namespace llvm;
31
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000032namespace {
33class SubtargetEmitter {
Andrew Trick52c3a1d2012-09-17 22:18:48 +000034 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
35 // The SchedClassDesc table indexes into a global write resource table, write
36 // latency table, and read advance table.
37 struct SchedClassTables {
38 std::vector<std::vector<MCSchedClassDesc> > ProcSchedClasses;
39 std::vector<MCWriteProcResEntry> WriteProcResources;
40 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trick3b8fb642012-09-19 04:43:19 +000041 std::vector<std::string> WriterNames;
Andrew Trick52c3a1d2012-09-17 22:18:48 +000042 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
43
44 // Reserve an invalid entry at index 0
45 SchedClassTables() {
46 ProcSchedClasses.resize(1);
47 WriteProcResources.resize(1);
48 WriteLatencies.resize(1);
Andrew Trick3b8fb642012-09-19 04:43:19 +000049 WriterNames.push_back("InvalidWrite");
Andrew Trick52c3a1d2012-09-17 22:18:48 +000050 ReadAdvanceEntries.resize(1);
51 }
52 };
53
54 struct LessWriteProcResources {
55 bool operator()(const MCWriteProcResEntry &LHS,
56 const MCWriteProcResEntry &RHS) {
57 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
58 }
59 };
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000060
61 RecordKeeper &Records;
Andrew Trick2661b412012-07-07 04:00:00 +000062 CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000063 std::string Target;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000064
65 void Enumeration(raw_ostream &OS, const char *ClassName, bool isBits);
66 unsigned FeatureKeyValues(raw_ostream &OS);
67 unsigned CPUKeyValues(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000068 void FormItineraryStageString(const std::string &Names,
69 Record *ItinData, std::string &ItinString,
70 unsigned &NStages);
71 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
72 unsigned &NOperandCycles);
73 void FormItineraryBypassString(const std::string &Names,
74 Record *ItinData,
75 std::string &ItinString, unsigned NOperandCycles);
Andrew Trick2661b412012-07-07 04:00:00 +000076 void EmitStageAndOperandCycleData(raw_ostream &OS,
77 std::vector<std::vector<InstrItinerary> >
78 &ProcItinLists);
79 void EmitItineraries(raw_ostream &OS,
80 std::vector<std::vector<InstrItinerary> >
81 &ProcItinLists);
82 void EmitProcessorProp(raw_ostream &OS, const Record *R, const char *Name,
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000083 char Separator);
Andrew Trick40096d22012-09-17 22:18:45 +000084 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
85 raw_ostream &OS);
Andrew Trick92649882012-09-22 02:24:21 +000086 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
Andrew Trick52c3a1d2012-09-17 22:18:48 +000087 const CodeGenProcModel &ProcModel);
Andrew Trick92649882012-09-22 02:24:21 +000088 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
89 const CodeGenProcModel &ProcModel);
Andrew Trick1754aca2013-03-14 21:21:50 +000090 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
91 const CodeGenProcModel &ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +000092 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
93 SchedClassTables &SchedTables);
Andrew Trick544c8802012-09-17 22:18:50 +000094 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
Andrew Trick2661b412012-07-07 04:00:00 +000095 void EmitProcessorModels(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000096 void EmitProcessorLookup(raw_ostream &OS);
Andrew Trick4d2d1c42012-09-18 03:41:43 +000097 void EmitSchedModelHelpers(std::string ClassName, raw_ostream &OS);
Andrew Trick2661b412012-07-07 04:00:00 +000098 void EmitSchedModel(raw_ostream &OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000099 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
100 unsigned NumProcs);
101
102public:
Andrew Trick2661b412012-07-07 04:00:00 +0000103 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT):
104 Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000105
106 void run(raw_ostream &o);
107
108};
109} // End anonymous namespace
110
Jim Laskey7dc02042005-10-22 07:59:56 +0000111//
Jim Laskey581a8f72005-10-26 17:30:34 +0000112// Enumeration - Emit the specified class as an enumeration.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000113//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000114void SubtargetEmitter::Enumeration(raw_ostream &OS,
Jim Laskey581a8f72005-10-26 17:30:34 +0000115 const char *ClassName,
116 bool isBits) {
Jim Laskey908ae272005-10-28 15:20:43 +0000117 // Get all records of class and sort
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000118 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
Duraid Madina42d24c72005-12-30 14:56:37 +0000119 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000120
Evan Chengb6a63882011-04-15 19:35:46 +0000121 unsigned N = DefList.size();
Evan Cheng94214702011-07-01 20:45:01 +0000122 if (N == 0)
123 return;
Evan Chengb6a63882011-04-15 19:35:46 +0000124 if (N > 64) {
125 errs() << "Too many (> 64) subtarget features!\n";
126 exit(1);
127 }
128
Evan Cheng94214702011-07-01 20:45:01 +0000129 OS << "namespace " << Target << " {\n";
130
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000131 // For bit flag enumerations with more than 32 items, emit constants.
132 // Emit an enum for everything else.
133 if (isBits && N > 32) {
134 // For each record
135 for (unsigned i = 0; i < N; i++) {
136 // Next record
137 Record *Def = DefList[i];
Evan Cheng94214702011-07-01 20:45:01 +0000138
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000139 // Get and emit name and expression (1 << i)
140 OS << " const uint64_t " << Def->getName() << " = 1ULL << " << i << ";\n";
141 }
142 } else {
143 // Open enumeration
144 OS << "enum {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000145
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000146 // For each record
147 for (unsigned i = 0; i < N;) {
148 // Next record
149 Record *Def = DefList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000150
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000151 // Get and emit name
152 OS << " " << Def->getName();
Jim Laskey908ae272005-10-28 15:20:43 +0000153
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000154 // If bit flags then emit expression (1 << i)
155 if (isBits) OS << " = " << " 1ULL << " << i;
Andrew Trickda96cf22011-04-01 01:56:55 +0000156
Jakob Stoklund Olesenac1ed442012-01-03 23:04:28 +0000157 // Depending on 'if more in the list' emit comma
158 if (++i < N) OS << ",";
159
160 OS << "\n";
161 }
162
163 // Close enumeration
164 OS << "};\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000165 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000166
Evan Cheng94214702011-07-01 20:45:01 +0000167 OS << "}\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000168}
169
170//
Bill Wendling4222d802007-05-04 20:38:40 +0000171// FeatureKeyValues - Emit data of all the subtarget features. Used by the
172// command line.
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000173//
Evan Cheng94214702011-07-01 20:45:01 +0000174unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000175 // Gather and sort all the features
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000176 std::vector<Record*> FeatureList =
177 Records.getAllDerivedDefinitions("SubtargetFeature");
Evan Cheng94214702011-07-01 20:45:01 +0000178
179 if (FeatureList.empty())
180 return 0;
181
Jim Grosbach7c9a7722008-09-11 17:05:32 +0000182 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000183
Jim Laskey908ae272005-10-28 15:20:43 +0000184 // Begin feature table
Jim Laskey581a8f72005-10-26 17:30:34 +0000185 OS << "// Sorted (by key) array of values for CPU features.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000186 << "extern const llvm::SubtargetFeatureKV " << Target
187 << "FeatureKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000188
Jim Laskey908ae272005-10-28 15:20:43 +0000189 // For each feature
Evan Cheng94214702011-07-01 20:45:01 +0000190 unsigned NumFeatures = 0;
Jim Laskeydbe40062006-12-12 20:55:58 +0000191 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000192 // Next feature
193 Record *Feature = FeatureList[i];
194
Bill Wendling4222d802007-05-04 20:38:40 +0000195 const std::string &Name = Feature->getName();
196 const std::string &CommandLineName = Feature->getValueAsString("Name");
197 const std::string &Desc = Feature->getValueAsString("Desc");
Andrew Trickda96cf22011-04-01 01:56:55 +0000198
Jim Laskeydbe40062006-12-12 20:55:58 +0000199 if (CommandLineName.empty()) continue;
Andrew Trickda96cf22011-04-01 01:56:55 +0000200
Jim Grosbachda4231f2009-03-26 16:17:51 +0000201 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000202 OS << " { "
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000203 << "\"" << CommandLineName << "\", "
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000204 << "\"" << Desc << "\", "
Evan Cheng94214702011-07-01 20:45:01 +0000205 << Target << "::" << Name << ", ";
Bill Wendling4222d802007-05-04 20:38:40 +0000206
Andrew Trickda96cf22011-04-01 01:56:55 +0000207 const std::vector<Record*> &ImpliesList =
Bill Wendling4222d802007-05-04 20:38:40 +0000208 Feature->getValueAsListOfDefs("Implies");
Andrew Trickda96cf22011-04-01 01:56:55 +0000209
Bill Wendling4222d802007-05-04 20:38:40 +0000210 if (ImpliesList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000211 OS << "0ULL";
Bill Wendling4222d802007-05-04 20:38:40 +0000212 } else {
213 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000214 OS << Target << "::" << ImpliesList[j]->getName();
Bill Wendling4222d802007-05-04 20:38:40 +0000215 if (++j < M) OS << " | ";
216 }
217 }
218
219 OS << " }";
Evan Cheng94214702011-07-01 20:45:01 +0000220 ++NumFeatures;
Andrew Trickda96cf22011-04-01 01:56:55 +0000221
Jim Laskey10b1dd92005-10-31 17:16:01 +0000222 // Depending on 'if more in the list' emit comma
Jim Laskeydbe40062006-12-12 20:55:58 +0000223 if ((i + 1) < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000224
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000225 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000226 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000227
Jim Laskey908ae272005-10-28 15:20:43 +0000228 // End feature table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000229 OS << "};\n";
230
Evan Cheng94214702011-07-01 20:45:01 +0000231 return NumFeatures;
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000232}
233
234//
235// CPUKeyValues - Emit data of all the subtarget processors. Used by command
236// line.
237//
Evan Cheng94214702011-07-01 20:45:01 +0000238unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Jim Laskey908ae272005-10-28 15:20:43 +0000239 // Gather and sort processor information
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000240 std::vector<Record*> ProcessorList =
241 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +0000242 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000243
Jim Laskey908ae272005-10-28 15:20:43 +0000244 // Begin processor table
Jim Laskey581a8f72005-10-26 17:30:34 +0000245 OS << "// Sorted (by key) array of values for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000246 << "extern const llvm::SubtargetFeatureKV " << Target
247 << "SubTypeKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000248
Jim Laskey908ae272005-10-28 15:20:43 +0000249 // For each processor
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000250 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
251 // Next processor
252 Record *Processor = ProcessorList[i];
253
Bill Wendling4222d802007-05-04 20:38:40 +0000254 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trickda96cf22011-04-01 01:56:55 +0000255 const std::vector<Record*> &FeatureList =
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000256 Processor->getValueAsListOfDefs("Features");
Andrew Trickda96cf22011-04-01 01:56:55 +0000257
Jim Laskey908ae272005-10-28 15:20:43 +0000258 // Emit as { "cpu", "description", f1 | f2 | ... fn },
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000259 OS << " { "
260 << "\"" << Name << "\", "
261 << "\"Select the " << Name << " processor\", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000262
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000263 if (FeatureList.empty()) {
Evan Chengb6a63882011-04-15 19:35:46 +0000264 OS << "0ULL";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000265 } else {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000266 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
Evan Cheng94214702011-07-01 20:45:01 +0000267 OS << Target << "::" << FeatureList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000268 if (++j < M) OS << " | ";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000269 }
270 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000271
Bill Wendling4222d802007-05-04 20:38:40 +0000272 // The "0" is for the "implies" section of this data structure.
Evan Chengb6a63882011-04-15 19:35:46 +0000273 OS << ", 0ULL }";
Andrew Trickda96cf22011-04-01 01:56:55 +0000274
Jim Laskey10b1dd92005-10-31 17:16:01 +0000275 // Depending on 'if more in the list' emit comma
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000276 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +0000277
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000278 OS << "\n";
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000279 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000280
Jim Laskey908ae272005-10-28 15:20:43 +0000281 // End processor table
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000282 OS << "};\n";
283
Evan Cheng94214702011-07-01 20:45:01 +0000284 return ProcessorList.size();
Jim Laskeyb3b1d5f2005-10-25 15:16:36 +0000285}
Jim Laskey7dc02042005-10-22 07:59:56 +0000286
Jim Laskey581a8f72005-10-26 17:30:34 +0000287//
David Goodwinfac85412009-08-17 16:02:57 +0000288// FormItineraryStageString - Compose a string containing the stage
289// data initialization for the specified itinerary. N is the number
290// of stages.
Jim Laskey0d841e02005-10-27 19:47:21 +0000291//
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000292void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
293 Record *ItinData,
David Goodwinfac85412009-08-17 16:02:57 +0000294 std::string &ItinString,
295 unsigned &NStages) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000296 // Get states list
Bill Wendling4222d802007-05-04 20:38:40 +0000297 const std::vector<Record*> &StageList =
298 ItinData->getValueAsListOfDefs("Stages");
Jim Laskey908ae272005-10-28 15:20:43 +0000299
300 // For each stage
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000301 unsigned N = NStages = StageList.size();
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000302 for (unsigned i = 0; i < N;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000303 // Next stage
Bill Wendling4222d802007-05-04 20:38:40 +0000304 const Record *Stage = StageList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000305
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000306 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Jim Laskey0d841e02005-10-27 19:47:21 +0000307 int Cycles = Stage->getValueAsInt("Cycles");
Jim Laskey7f39c142005-11-03 22:47:41 +0000308 ItinString += " { " + itostr(Cycles) + ", ";
Andrew Trickda96cf22011-04-01 01:56:55 +0000309
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000310 // Get unit list
Bill Wendling4222d802007-05-04 20:38:40 +0000311 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
Andrew Trickda96cf22011-04-01 01:56:55 +0000312
Jim Laskey908ae272005-10-28 15:20:43 +0000313 // For each unit
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000314 for (unsigned j = 0, M = UnitList.size(); j < M;) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000315 // Add name and bitwise or
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000316 ItinString += Name + "FU::" + UnitList[j]->getName();
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000317 if (++j < M) ItinString += " | ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000318 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000319
David Goodwin1a8f36e2009-08-12 18:31:53 +0000320 int TimeInc = Stage->getValueAsInt("TimeInc");
321 ItinString += ", " + itostr(TimeInc);
322
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000323 int Kind = Stage->getValueAsInt("Kind");
324 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
325
Jim Laskey908ae272005-10-28 15:20:43 +0000326 // Close off stage
327 ItinString += " }";
Christopher Lamb8dadf6b2007-04-22 09:04:24 +0000328 if (++i < N) ItinString += ", ";
Jim Laskey0d841e02005-10-27 19:47:21 +0000329 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000330}
331
332//
David Goodwinfac85412009-08-17 16:02:57 +0000333// FormItineraryOperandCycleString - Compose a string containing the
334// operand cycle initialization for the specified itinerary. N is the
335// number of operands that has cycles specified.
Jim Laskey0d841e02005-10-27 19:47:21 +0000336//
David Goodwinfac85412009-08-17 16:02:57 +0000337void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
338 std::string &ItinString, unsigned &NOperandCycles) {
339 // Get operand cycle list
340 const std::vector<int64_t> &OperandCycleList =
341 ItinData->getValueAsListOfInts("OperandCycles");
342
343 // For each operand cycle
344 unsigned N = NOperandCycles = OperandCycleList.size();
345 for (unsigned i = 0; i < N;) {
346 // Next operand cycle
347 const int OCycle = OperandCycleList[i];
Andrew Trickda96cf22011-04-01 01:56:55 +0000348
David Goodwinfac85412009-08-17 16:02:57 +0000349 ItinString += " " + itostr(OCycle);
350 if (++i < N) ItinString += ", ";
351 }
352}
353
Evan Cheng63d66ee2010-09-28 23:50:49 +0000354void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
355 Record *ItinData,
356 std::string &ItinString,
357 unsigned NOperandCycles) {
358 const std::vector<Record*> &BypassList =
359 ItinData->getValueAsListOfDefs("Bypasses");
360 unsigned N = BypassList.size();
Evan Cheng3881cb72010-09-29 22:42:35 +0000361 unsigned i = 0;
362 for (; i < N;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000363 ItinString += Name + "Bypass::" + BypassList[i]->getName();
Evan Cheng3881cb72010-09-29 22:42:35 +0000364 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000365 }
Evan Cheng3881cb72010-09-29 22:42:35 +0000366 for (; i < NOperandCycles;) {
Evan Cheng63d66ee2010-09-28 23:50:49 +0000367 ItinString += " 0";
Evan Cheng3881cb72010-09-29 22:42:35 +0000368 if (++i < NOperandCycles) ItinString += ", ";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000369 }
370}
371
David Goodwinfac85412009-08-17 16:02:57 +0000372//
Andrew Trick2661b412012-07-07 04:00:00 +0000373// EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
374// cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
375// by CodeGenSchedClass::Index.
David Goodwinfac85412009-08-17 16:02:57 +0000376//
Andrew Trick2661b412012-07-07 04:00:00 +0000377void SubtargetEmitter::
378EmitStageAndOperandCycleData(raw_ostream &OS,
379 std::vector<std::vector<InstrItinerary> >
380 &ProcItinLists) {
Jim Laskey908ae272005-10-28 15:20:43 +0000381
Andrew Trickcb941922012-07-09 20:43:03 +0000382 // Multiple processor models may share an itinerary record. Emit it once.
383 SmallPtrSet<Record*, 8> ItinsDefSet;
384
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000385 // Emit functional units for all the itineraries.
Andrew Trick2661b412012-07-07 04:00:00 +0000386 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
387 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000388
Andrew Trickcb941922012-07-09 20:43:03 +0000389 if (!ItinsDefSet.insert(PI->ItinsDef))
390 continue;
391
Andrew Trick2661b412012-07-07 04:00:00 +0000392 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000393 if (FUs.empty())
394 continue;
395
Andrew Trick2661b412012-07-07 04:00:00 +0000396 const std::string &Name = PI->ItinsDef->getName();
397 OS << "\n// Functional units for \"" << Name << "\"\n"
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000398 << "namespace " << Name << "FU {\n";
399
400 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
Hal Finkelb460a332012-06-22 20:27:13 +0000401 OS << " const unsigned " << FUs[j]->getName()
402 << " = 1 << " << j << ";\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000403
404 OS << "}\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000405
Andrew Trick2661b412012-07-07 04:00:00 +0000406 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
Evan Cheng3881cb72010-09-29 22:42:35 +0000407 if (BPs.size()) {
408 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
409 << "\"\n" << "namespace " << Name << "Bypass {\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000410
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000411 OS << " const unsigned NoBypass = 0;\n";
Evan Cheng3881cb72010-09-29 22:42:35 +0000412 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000413 OS << " const unsigned " << BPs[j]->getName()
Evan Cheng3881cb72010-09-29 22:42:35 +0000414 << " = 1 << " << j << ";\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000415
Evan Cheng3881cb72010-09-29 22:42:35 +0000416 OS << "}\n";
417 }
Anton Korobeynikov928eb492010-04-18 20:31:01 +0000418 }
419
Jim Laskey908ae272005-10-28 15:20:43 +0000420 // Begin stages table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000421 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
422 "Stages[] = {\n";
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000423 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000424
David Goodwinfac85412009-08-17 16:02:57 +0000425 // Begin operand cycle table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000426 std::string OperandCycleTable = "extern const unsigned " + Target +
Evan Cheng94214702011-07-01 20:45:01 +0000427 "OperandCycles[] = {\n";
David Goodwinfac85412009-08-17 16:02:57 +0000428 OperandCycleTable += " 0, // No itinerary\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000429
430 // Begin pipeline bypass table
Benjamin Kramer1a2f9882011-10-22 16:50:00 +0000431 std::string BypassTable = "extern const unsigned " + Target +
Andrew Tricka11a6282012-07-07 03:59:48 +0000432 "ForwardingPaths[] = {\n";
Andrew Trick2661b412012-07-07 04:00:00 +0000433 BypassTable += " 0, // No itinerary\n";
Andrew Trickda96cf22011-04-01 01:56:55 +0000434
Andrew Trick2661b412012-07-07 04:00:00 +0000435 // For each Itinerary across all processors, add a unique entry to the stages,
436 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
437 // object with computed offsets to the ProcItinLists result.
David Goodwinfac85412009-08-17 16:02:57 +0000438 unsigned StageCount = 1, OperandCycleCount = 1;
Evan Cheng3881cb72010-09-29 22:42:35 +0000439 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
Andrew Trick2661b412012-07-07 04:00:00 +0000440 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
441 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
442 const CodeGenProcModel &ProcModel = *PI;
Andrew Trickda96cf22011-04-01 01:56:55 +0000443
Andrew Trick2661b412012-07-07 04:00:00 +0000444 // Add process itinerary to the list.
445 ProcItinLists.resize(ProcItinLists.size()+1);
Andrew Trickda96cf22011-04-01 01:56:55 +0000446
Andrew Trick2661b412012-07-07 04:00:00 +0000447 // If this processor defines no itineraries, then leave the itinerary list
448 // empty.
449 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
Andrew Trick1ab961f2013-03-16 18:58:55 +0000450 if (!ProcModel.hasItineraries())
Andrew Trickd85934b2012-06-22 03:58:51 +0000451 continue;
Andrew Trickd85934b2012-06-22 03:58:51 +0000452
Andrew Trick2661b412012-07-07 04:00:00 +0000453 const std::string &Name = ProcModel.ItinsDef->getName();
Andrew Trickda96cf22011-04-01 01:56:55 +0000454
Andrew Trick1ab961f2013-03-16 18:58:55 +0000455 ItinList.resize(SchedModels.numInstrSchedClasses());
456 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
457
458 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
Andrew Trick2661b412012-07-07 04:00:00 +0000459 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
460
Jim Laskeyf7bcde02005-10-28 21:47:29 +0000461 // Next itinerary data
Andrew Trick2661b412012-07-07 04:00:00 +0000462 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
Andrew Trickda96cf22011-04-01 01:56:55 +0000463
Jim Laskey908ae272005-10-28 15:20:43 +0000464 // Get string and stage count
David Goodwinfac85412009-08-17 16:02:57 +0000465 std::string ItinStageString;
Andrew Trick2661b412012-07-07 04:00:00 +0000466 unsigned NStages = 0;
467 if (ItinData)
468 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Jim Laskey0d841e02005-10-27 19:47:21 +0000469
David Goodwinfac85412009-08-17 16:02:57 +0000470 // Get string and operand cycle count
471 std::string ItinOperandCycleString;
Andrew Trick2661b412012-07-07 04:00:00 +0000472 unsigned NOperandCycles = 0;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000473 std::string ItinBypassString;
Andrew Trick2661b412012-07-07 04:00:00 +0000474 if (ItinData) {
475 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
476 NOperandCycles);
477
478 FormItineraryBypassString(Name, ItinData, ItinBypassString,
479 NOperandCycles);
480 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000481
David Goodwinfac85412009-08-17 16:02:57 +0000482 // Check to see if stage already exists and create if it doesn't
483 unsigned FindStage = 0;
484 if (NStages > 0) {
485 FindStage = ItinStageMap[ItinStageString];
486 if (FindStage == 0) {
Andrew Trick23482322011-04-01 02:22:47 +0000487 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
488 StageTable += ItinStageString + ", // " + itostr(StageCount);
489 if (NStages > 1)
490 StageTable += "-" + itostr(StageCount + NStages - 1);
491 StageTable += "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000492 // Record Itin class number.
493 ItinStageMap[ItinStageString] = FindStage = StageCount;
494 StageCount += NStages;
David Goodwinfac85412009-08-17 16:02:57 +0000495 }
496 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000497
David Goodwinfac85412009-08-17 16:02:57 +0000498 // Check to see if operand cycle already exists and create if it doesn't
499 unsigned FindOperandCycle = 0;
500 if (NOperandCycles > 0) {
Evan Cheng3881cb72010-09-29 22:42:35 +0000501 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
502 FindOperandCycle = ItinOperandMap[ItinOperandString];
David Goodwinfac85412009-08-17 16:02:57 +0000503 if (FindOperandCycle == 0) {
504 // Emit as cycle, // index
Andrew Trick23482322011-04-01 02:22:47 +0000505 OperandCycleTable += ItinOperandCycleString + ", // ";
506 std::string OperandIdxComment = itostr(OperandCycleCount);
507 if (NOperandCycles > 1)
508 OperandIdxComment += "-"
509 + itostr(OperandCycleCount + NOperandCycles - 1);
510 OperandCycleTable += OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000511 // Record Itin class number.
Andrew Trickda96cf22011-04-01 01:56:55 +0000512 ItinOperandMap[ItinOperandCycleString] =
David Goodwinfac85412009-08-17 16:02:57 +0000513 FindOperandCycle = OperandCycleCount;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000514 // Emit as bypass, // index
Andrew Trick23482322011-04-01 02:22:47 +0000515 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
David Goodwinfac85412009-08-17 16:02:57 +0000516 OperandCycleCount += NOperandCycles;
David Goodwinfac85412009-08-17 16:02:57 +0000517 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000518 }
Andrew Trickda96cf22011-04-01 01:56:55 +0000519
Evan Cheng5f54ce32010-09-09 18:18:55 +0000520 // Set up itinerary as location and location + stage count
Andrew Trick2661b412012-07-07 04:00:00 +0000521 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
Evan Cheng5f54ce32010-09-09 18:18:55 +0000522 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
523 FindOperandCycle,
524 FindOperandCycle + NOperandCycles};
525
Jim Laskey908ae272005-10-28 15:20:43 +0000526 // Inject - empty slots will be 0, 0
Andrew Trick2661b412012-07-07 04:00:00 +0000527 ItinList[SchedClassIdx] = Intinerary;
Jim Laskey0d841e02005-10-27 19:47:21 +0000528 }
Jim Laskey0d841e02005-10-27 19:47:21 +0000529 }
Evan Cheng63d66ee2010-09-28 23:50:49 +0000530
Jim Laskey7f39c142005-11-03 22:47:41 +0000531 // Closing stage
Andrew Trick2661b412012-07-07 04:00:00 +0000532 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
David Goodwinfac85412009-08-17 16:02:57 +0000533 StageTable += "};\n";
534
535 // Closing operand cycles
Andrew Trick2661b412012-07-07 04:00:00 +0000536 OperandCycleTable += " 0 // End operand cycles\n";
David Goodwinfac85412009-08-17 16:02:57 +0000537 OperandCycleTable += "};\n";
538
Andrew Trick2661b412012-07-07 04:00:00 +0000539 BypassTable += " 0 // End bypass tables\n";
Evan Cheng63d66ee2010-09-28 23:50:49 +0000540 BypassTable += "};\n";
541
David Goodwinfac85412009-08-17 16:02:57 +0000542 // Emit tables.
543 OS << StageTable;
544 OS << OperandCycleTable;
Evan Cheng63d66ee2010-09-28 23:50:49 +0000545 OS << BypassTable;
Jim Laskey0d841e02005-10-27 19:47:21 +0000546}
547
Andrew Trick2661b412012-07-07 04:00:00 +0000548//
549// EmitProcessorData - Generate data for processor itineraries that were
550// computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
551// Itineraries for each processor. The Itinerary lists are indexed on
552// CodeGenSchedClass::Index.
553//
554void SubtargetEmitter::
555EmitItineraries(raw_ostream &OS,
556 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
557
Andrew Trickcb941922012-07-09 20:43:03 +0000558 // Multiple processor models may share an itinerary record. Emit it once.
559 SmallPtrSet<Record*, 8> ItinsDefSet;
560
Andrew Trick2661b412012-07-07 04:00:00 +0000561 // For each processor's machine model
562 std::vector<std::vector<InstrItinerary> >::iterator
563 ProcItinListsIter = ProcItinLists.begin();
564 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
Andrew Trick48605c32012-09-15 00:19:57 +0000565 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
Andrew Trickcb941922012-07-09 20:43:03 +0000566
Andrew Trick2661b412012-07-07 04:00:00 +0000567 Record *ItinsDef = PI->ItinsDef;
Andrew Trickcb941922012-07-09 20:43:03 +0000568 if (!ItinsDefSet.insert(ItinsDef))
569 continue;
Andrew Trick2661b412012-07-07 04:00:00 +0000570
571 // Get processor itinerary name
572 const std::string &Name = ItinsDef->getName();
573
574 // Get the itinerary list for the processor.
575 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
Andrew Trick48605c32012-09-15 00:19:57 +0000576 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
Andrew Trick2661b412012-07-07 04:00:00 +0000577
578 OS << "\n";
579 OS << "static const llvm::InstrItinerary ";
580 if (ItinList.empty()) {
581 OS << '*' << Name << " = 0;\n";
582 continue;
583 }
584
585 // Begin processor itinerary table
586 OS << Name << "[] = {\n";
587
588 // For each itinerary class in CodeGenSchedClass::Index order.
589 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
590 InstrItinerary &Intinerary = ItinList[j];
591
592 // Emit Itinerary in the form of
593 // { firstStage, lastStage, firstCycle, lastCycle } // index
594 OS << " { " <<
595 Intinerary.NumMicroOps << ", " <<
596 Intinerary.FirstStage << ", " <<
597 Intinerary.LastStage << ", " <<
598 Intinerary.FirstOperandCycle << ", " <<
599 Intinerary.LastOperandCycle << " }" <<
600 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
601 }
602 // End processor itinerary table
603 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
604 OS << "};\n";
605 }
606}
607
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000608// Emit either the value defined in the TableGen Record, or the default
Andrew Trick2661b412012-07-07 04:00:00 +0000609// value defined in the C++ header. The Record is null if the processor does not
610// define a model.
611void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
Andrew Trickfc992992012-06-05 03:44:40 +0000612 const char *Name, char Separator) {
613 OS << " ";
Andrew Trick2661b412012-07-07 04:00:00 +0000614 int V = R ? R->getValueAsInt(Name) : -1;
Andrew Trickfc992992012-06-05 03:44:40 +0000615 if (V >= 0)
616 OS << V << Separator << " // " << Name;
617 else
Andrew Trick2661b412012-07-07 04:00:00 +0000618 OS << "MCSchedModel::Default" << Name << Separator;
Andrew Trickfc992992012-06-05 03:44:40 +0000619 OS << '\n';
620}
621
Andrew Trick40096d22012-09-17 22:18:45 +0000622void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
623 raw_ostream &OS) {
624 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
625
Andrew Trick6312cb02012-10-10 05:43:04 +0000626 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n";
Andrew Trick40096d22012-09-17 22:18:45 +0000627 OS << "static const llvm::MCProcResourceDesc "
628 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
Andrew Trick6312cb02012-10-10 05:43:04 +0000629 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n";
Andrew Trick40096d22012-09-17 22:18:45 +0000630
631 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
632 Record *PRDef = ProcModel.ProcResourceDefs[i];
633
Andrew Trick40096d22012-09-17 22:18:45 +0000634 Record *SuperDef = 0;
Andrew Trick1754aca2013-03-14 21:21:50 +0000635 unsigned SuperIdx = 0;
636 unsigned NumUnits = 0;
637 bool IsBuffered = true;
638 if (PRDef->isSubClassOf("ProcResGroup")) {
639 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
640 for (RecIter RUI = ResUnits.begin(), RUE = ResUnits.end();
641 RUI != RUE; ++RUI) {
642 if (!NumUnits)
643 IsBuffered = (*RUI)->getValueAsBit("Buffered");
644 else if(IsBuffered != (*RUI)->getValueAsBit("Buffered"))
645 PrintFatalError(PRDef->getLoc(),
646 "Mixing buffered and unbuffered resources.");
647 NumUnits += (*RUI)->getValueAsInt("NumUnits");
648 }
649 }
650 else {
651 // Find the SuperIdx
652 if (PRDef->getValueInit("Super")->isComplete()) {
653 SuperDef = SchedModels.findProcResUnits(
654 PRDef->getValueAsDef("Super"), ProcModel);
655 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
656 }
Andrew Trick157c6c42013-03-14 22:47:01 +0000657 NumUnits = PRDef->getValueAsInt("NumUnits");
658 IsBuffered = PRDef->getValueAsBit("Buffered");
Andrew Trick40096d22012-09-17 22:18:45 +0000659 }
660 // Emit the ProcResourceDesc
661 if (i+1 == e)
662 Sep = ' ';
663 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
664 if (PRDef->getName().size() < 15)
665 OS.indent(15 - PRDef->getName().size());
Andrew Trick1754aca2013-03-14 21:21:50 +0000666 OS << NumUnits << ", " << SuperIdx << ", "
667 << IsBuffered << "}" << Sep << " // #" << i+1;
Andrew Trick40096d22012-09-17 22:18:45 +0000668 if (SuperDef)
669 OS << ", Super=" << SuperDef->getName();
670 OS << "\n";
671 }
672 OS << "};\n";
673}
674
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000675// Find the WriteRes Record that defines processor resources for this
676// SchedWrite.
677Record *SubtargetEmitter::FindWriteResources(
Andrew Trick92649882012-09-22 02:24:21 +0000678 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000679
680 // Check if the SchedWrite is already subtarget-specific and directly
681 // specifies a set of processor resources.
Andrew Trick92649882012-09-22 02:24:21 +0000682 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
683 return SchedWrite.TheDef;
684
Andrew Trick92649882012-09-22 02:24:21 +0000685 Record *AliasDef = 0;
686 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
687 AI != AE; ++AI) {
688 const CodeGenSchedRW &AliasRW =
689 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trick2062b122012-10-03 23:06:28 +0000690 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
691 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
692 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
693 continue;
694 }
Andrew Trick92649882012-09-22 02:24:21 +0000695 if (AliasDef)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000696 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick92649882012-09-22 02:24:21 +0000697 "defined for processor " + ProcModel.ModelName +
698 " Ensure only one SchedAlias exists per RW.");
699 AliasDef = AliasRW.TheDef;
700 }
701 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
702 return AliasDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000703
704 // Check this processor's list of write resources.
Andrew Trick92649882012-09-22 02:24:21 +0000705 Record *ResDef = 0;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000706 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
707 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
708 if (!(*WRI)->isSubClassOf("WriteRes"))
709 continue;
Andrew Trick92649882012-09-22 02:24:21 +0000710 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
711 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
712 if (ResDef) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000713 PrintFatalError((*WRI)->getLoc(), "Resources are defined for both "
Andrew Trick92649882012-09-22 02:24:21 +0000714 "SchedWrite and its alias on processor " +
715 ProcModel.ModelName);
716 }
717 ResDef = *WRI;
718 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000719 }
Andrew Trick92649882012-09-22 02:24:21 +0000720 // TODO: If ProcModel has a base model (previous generation processor),
721 // then call FindWriteResources recursively with that model here.
722 if (!ResDef) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000723 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick92649882012-09-22 02:24:21 +0000724 std::string("Processor does not define resources for ")
725 + SchedWrite.TheDef->getName());
726 }
727 return ResDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000728}
729
730/// Find the ReadAdvance record for the given SchedRead on this processor or
731/// return NULL.
Andrew Trick92649882012-09-22 02:24:21 +0000732Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000733 const CodeGenProcModel &ProcModel) {
734 // Check for SchedReads that directly specify a ReadAdvance.
Andrew Trick92649882012-09-22 02:24:21 +0000735 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
736 return SchedRead.TheDef;
737
738 // Check this processor's list of aliases for SchedRead.
739 Record *AliasDef = 0;
740 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
741 AI != AE; ++AI) {
742 const CodeGenSchedRW &AliasRW =
743 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
Andrew Trick2062b122012-10-03 23:06:28 +0000744 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
745 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
746 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
747 continue;
748 }
Andrew Trick92649882012-09-22 02:24:21 +0000749 if (AliasDef)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000750 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
Andrew Trick92649882012-09-22 02:24:21 +0000751 "defined for processor " + ProcModel.ModelName +
752 " Ensure only one SchedAlias exists per RW.");
753 AliasDef = AliasRW.TheDef;
754 }
755 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
756 return AliasDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000757
758 // Check this processor's ReadAdvanceList.
Andrew Trick92649882012-09-22 02:24:21 +0000759 Record *ResDef = 0;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000760 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
761 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
762 if (!(*RAI)->isSubClassOf("ReadAdvance"))
763 continue;
Andrew Trick92649882012-09-22 02:24:21 +0000764 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
765 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
766 if (ResDef) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000767 PrintFatalError((*RAI)->getLoc(), "Resources are defined for both "
Andrew Trick92649882012-09-22 02:24:21 +0000768 "SchedRead and its alias on processor " +
769 ProcModel.ModelName);
770 }
771 ResDef = *RAI;
772 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000773 }
Andrew Trick92649882012-09-22 02:24:21 +0000774 // TODO: If ProcModel has a base model (previous generation processor),
775 // then call FindReadAdvance recursively with that model here.
776 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000777 PrintFatalError(ProcModel.ModelDef->getLoc(),
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000778 std::string("Processor does not define resources for ")
Andrew Trick92649882012-09-22 02:24:21 +0000779 + SchedRead.TheDef->getName());
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000780 }
Andrew Trick92649882012-09-22 02:24:21 +0000781 return ResDef;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000782}
783
Andrew Trick1754aca2013-03-14 21:21:50 +0000784// Expand an explicit list of processor resources into a full list of implied
785// resource groups that cover them.
786//
787// FIXME: Effectively consider a super-resource a group that include all of its
788// subresources to allow mixing and matching super-resources and groups.
789//
790// FIXME: Warn if two overlapping groups don't have a common supergroup.
791void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
792 std::vector<int64_t> &Cycles,
793 const CodeGenProcModel &ProcModel) {
794 // Default to 1 resource cycle.
795 Cycles.resize(PRVec.size(), 1);
796 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
797 RecVec SubResources;
798 if (PRVec[i]->isSubClassOf("ProcResGroup")) {
799 SubResources = PRVec[i]->getValueAsListOfDefs("Resources");
Andrew Trick1754aca2013-03-14 21:21:50 +0000800 }
801 else {
802 SubResources.push_back(PRVec[i]);
803 }
804 for (RecIter PRI = ProcModel.ProcResourceDefs.begin(),
805 PRE = ProcModel.ProcResourceDefs.end();
806 PRI != PRE; ++PRI) {
807 if (*PRI == PRVec[i] || !(*PRI)->isSubClassOf("ProcResGroup"))
808 continue;
809 RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources");
Andrew Trick1754aca2013-03-14 21:21:50 +0000810 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6982bdd2013-04-23 23:45:11 +0000811 for( ; SubI != SubE; ++SubI) {
812 if (std::find(SuperResources.begin(), SuperResources.end(), *SubI)
813 == SuperResources.end()) {
Andrew Trick1754aca2013-03-14 21:21:50 +0000814 break;
Andrew Trick6982bdd2013-04-23 23:45:11 +0000815 }
Andrew Trick1754aca2013-03-14 21:21:50 +0000816 }
817 if (SubI == SubE) {
818 PRVec.push_back(*PRI);
819 Cycles.push_back(Cycles[i]);
820 }
821 }
822 }
823}
824
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000825// Generate the SchedClass table for this processor and update global
826// tables. Must be called for each processor in order.
827void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
828 SchedClassTables &SchedTables) {
829 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
830 if (!ProcModel.hasInstrSchedModel())
831 return;
832
833 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
834 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
835 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
Andrew Trickfe05d982012-10-03 23:06:25 +0000836 DEBUG(SCI->dump(&SchedModels));
837
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000838 SCTab.resize(SCTab.size() + 1);
839 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Tricke127dfd2012-09-18 03:18:56 +0000840 // SCDesc.Name is guarded by NDEBUG
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000841 SCDesc.NumMicroOps = 0;
842 SCDesc.BeginGroup = false;
843 SCDesc.EndGroup = false;
844 SCDesc.WriteProcResIdx = 0;
845 SCDesc.WriteLatencyIdx = 0;
846 SCDesc.ReadAdvanceIdx = 0;
847
848 // A Variant SchedClass has no resources of its own.
Andrew Trick82e7c4f2013-03-26 21:36:39 +0000849 bool HasVariants = false;
850 for (std::vector<CodeGenSchedTransition>::const_iterator
851 TI = SCI->Transitions.begin(), TE = SCI->Transitions.end();
852 TI != TE; ++TI) {
853 if (TI->ProcIndices[0] == 0) {
854 HasVariants = true;
855 break;
856 }
857 IdxIter PIPos = std::find(TI->ProcIndices.begin(),
858 TI->ProcIndices.end(), ProcModel.Index);
859 if (PIPos != TI->ProcIndices.end()) {
860 HasVariants = true;
861 break;
862 }
863 }
864 if (HasVariants) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000865 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
866 continue;
867 }
868
869 // Determine if the SchedClass is actually reachable on this processor. If
870 // not don't try to locate the processor resources, it will fail.
871 // If ProcIndices contains 0, this class applies to all processors.
872 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
873 if (SCI->ProcIndices[0] != 0) {
874 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
875 SCI->ProcIndices.end(), ProcModel.Index);
876 if (PIPos == SCI->ProcIndices.end())
877 continue;
878 }
879 IdxVec Writes = SCI->Writes;
880 IdxVec Reads = SCI->Reads;
Andrew Trick1ab961f2013-03-16 18:58:55 +0000881 if (!SCI->InstRWs.empty()) {
882 // This class has a default ReadWrite list which can be overriden by
Andrew Trickfe05d982012-10-03 23:06:25 +0000883 // InstRW definitions.
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000884 Record *RWDef = 0;
885 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
886 RWI != RWE; ++RWI) {
887 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
888 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
889 RWDef = *RWI;
890 break;
891 }
892 }
893 if (RWDef) {
Andrew Trick2062b122012-10-03 23:06:28 +0000894 Writes.clear();
895 Reads.clear();
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000896 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
897 Writes, Reads);
898 }
899 }
Andrew Trick1ab961f2013-03-16 18:58:55 +0000900 if (Writes.empty()) {
901 // Check this processor's itinerary class resources.
902 for (RecIter II = ProcModel.ItinRWDefs.begin(),
903 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
904 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
905 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
906 != Matched.end()) {
907 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
908 Writes, Reads);
909 break;
910 }
911 }
912 if (Writes.empty()) {
913 DEBUG(dbgs() << ProcModel.ModelName
914 << " does not have resources for class " << SCI->Name << '\n');
915 }
916 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000917 // Sum resources across all operand writes.
918 std::vector<MCWriteProcResEntry> WriteProcResources;
919 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trick3b8fb642012-09-19 04:43:19 +0000920 std::vector<std::string> WriterNames;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000921 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
922 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
923 IdxVec WriteSeq;
Andrew Trick2062b122012-10-03 23:06:28 +0000924 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
925 ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000926
927 // For each operand, create a latency entry.
928 MCWriteLatencyEntry WLEntry;
929 WLEntry.Cycles = 0;
Andrew Trick3b8fb642012-09-19 04:43:19 +0000930 unsigned WriteID = WriteSeq.back();
931 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
932 // If this Write is not referenced by a ReadAdvance, don't distinguish it
933 // from other WriteLatency entries.
Andrew Trick1ab961f2013-03-16 18:58:55 +0000934 if (!SchedModels.hasReadOfWrite(
935 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trick3b8fb642012-09-19 04:43:19 +0000936 WriteID = 0;
937 }
938 WLEntry.WriteResourceID = WriteID;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000939
940 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
941 WSI != WSE; ++WSI) {
942
Andrew Trick92649882012-09-22 02:24:21 +0000943 Record *WriteRes =
944 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000945
946 // Mark the parent class as invalid for unsupported write types.
947 if (WriteRes->getValueAsBit("Unsupported")) {
948 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
949 break;
950 }
951 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
952 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
953 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
954 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
955
956 // Create an entry for each ProcResource listed in WriteRes.
957 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
958 std::vector<int64_t> Cycles =
959 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick1754aca2013-03-14 21:21:50 +0000960
961 ExpandProcResources(PRVec, Cycles, ProcModel);
962
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000963 for (unsigned PRIdx = 0, PREnd = PRVec.size();
964 PRIdx != PREnd; ++PRIdx) {
965 MCWriteProcResEntry WPREntry;
966 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
967 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick1754aca2013-03-14 21:21:50 +0000968 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trickc8121102013-03-01 23:31:26 +0000969 // If this resource is already used in this sequence, add the current
970 // entry's cycles so that the same resource appears to be used
971 // serially, rather than multiple parallel uses. This is important for
972 // in-order machine where the resource consumption is a hazard.
973 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
974 for( ; WPRIdx != WPREnd; ++WPRIdx) {
975 if (WriteProcResources[WPRIdx].ProcResourceIdx
976 == WPREntry.ProcResourceIdx) {
977 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
978 break;
979 }
980 }
981 if (WPRIdx == WPREnd)
982 WriteProcResources.push_back(WPREntry);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000983 }
984 }
985 WriteLatencies.push_back(WLEntry);
986 }
987 // Create an entry for each operand Read in this SchedClass.
988 // Entries must be sorted first by UseIdx then by WriteResourceID.
989 for (unsigned UseIdx = 0, EndIdx = Reads.size();
990 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick92649882012-09-22 02:24:21 +0000991 Record *ReadAdvance =
992 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000993 if (!ReadAdvance)
994 continue;
995
996 // Mark the parent class as invalid for unsupported write types.
997 if (ReadAdvance->getValueAsBit("Unsupported")) {
998 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
999 break;
1000 }
1001 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1002 IdxVec WriteIDs;
1003 if (ValidWrites.empty())
1004 WriteIDs.push_back(0);
1005 else {
1006 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
1007 VWI != VWE; ++VWI) {
1008 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
1009 }
1010 }
1011 std::sort(WriteIDs.begin(), WriteIDs.end());
1012 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
1013 MCReadAdvanceEntry RAEntry;
1014 RAEntry.UseIdx = UseIdx;
1015 RAEntry.WriteResourceID = *WI;
1016 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1017 ReadAdvanceEntries.push_back(RAEntry);
1018 }
1019 }
1020 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1021 WriteProcResources.clear();
1022 WriteLatencies.clear();
1023 ReadAdvanceEntries.clear();
1024 }
1025 // Add the information for this SchedClass to the global tables using basic
1026 // compression.
1027 //
1028 // WritePrecRes entries are sorted by ProcResIdx.
1029 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1030 LessWriteProcResources());
1031
1032 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1033 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1034 std::search(SchedTables.WriteProcResources.begin(),
1035 SchedTables.WriteProcResources.end(),
1036 WriteProcResources.begin(), WriteProcResources.end());
1037 if (WPRPos != SchedTables.WriteProcResources.end())
1038 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1039 else {
1040 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1041 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1042 WriteProcResources.end());
1043 }
1044 // Latency entries must remain in operand order.
1045 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1046 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1047 std::search(SchedTables.WriteLatencies.begin(),
1048 SchedTables.WriteLatencies.end(),
1049 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trick3b8fb642012-09-19 04:43:19 +00001050 if (WLPos != SchedTables.WriteLatencies.end()) {
1051 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1052 SCDesc.WriteLatencyIdx = idx;
1053 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1054 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1055 std::string::npos) {
1056 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1057 }
1058 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001059 else {
1060 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trick3b8fb642012-09-19 04:43:19 +00001061 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1062 WriteLatencies.begin(),
1063 WriteLatencies.end());
1064 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1065 WriterNames.begin(), WriterNames.end());
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001066 }
1067 // ReadAdvanceEntries must remain in operand order.
1068 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1069 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1070 std::search(SchedTables.ReadAdvanceEntries.begin(),
1071 SchedTables.ReadAdvanceEntries.end(),
1072 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1073 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1074 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1075 else {
1076 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1077 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1078 ReadAdvanceEntries.end());
1079 }
1080 }
1081}
1082
Andrew Trick544c8802012-09-17 22:18:50 +00001083// Emit SchedClass tables for all processors and associated global tables.
1084void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1085 raw_ostream &OS) {
1086 // Emit global WriteProcResTable.
1087 OS << "\n// {ProcResourceIdx, Cycles}\n"
1088 << "extern const llvm::MCWriteProcResEntry "
1089 << Target << "WriteProcResTable[] = {\n"
1090 << " { 0, 0}, // Invalid\n";
1091 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1092 WPRIdx != WPREnd; ++WPRIdx) {
1093 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1094 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1095 << format("%2d", WPREntry.Cycles) << "}";
1096 if (WPRIdx + 1 < WPREnd)
1097 OS << ',';
1098 OS << " // #" << WPRIdx << '\n';
1099 }
1100 OS << "}; // " << Target << "WriteProcResTable\n";
1101
1102 // Emit global WriteLatencyTable.
1103 OS << "\n// {Cycles, WriteResourceID}\n"
1104 << "extern const llvm::MCWriteLatencyEntry "
1105 << Target << "WriteLatencyTable[] = {\n"
1106 << " { 0, 0}, // Invalid\n";
1107 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1108 WLIdx != WLEnd; ++WLIdx) {
1109 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1110 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1111 << format("%2d", WLEntry.WriteResourceID) << "}";
1112 if (WLIdx + 1 < WLEnd)
1113 OS << ',';
Andrew Trick3b8fb642012-09-19 04:43:19 +00001114 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Trick544c8802012-09-17 22:18:50 +00001115 }
1116 OS << "}; // " << Target << "WriteLatencyTable\n";
1117
1118 // Emit global ReadAdvanceTable.
1119 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1120 << "extern const llvm::MCReadAdvanceEntry "
1121 << Target << "ReadAdvanceTable[] = {\n"
1122 << " {0, 0, 0}, // Invalid\n";
1123 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1124 RAIdx != RAEnd; ++RAIdx) {
1125 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1126 OS << " {" << RAEntry.UseIdx << ", "
1127 << format("%2d", RAEntry.WriteResourceID) << ", "
1128 << format("%2d", RAEntry.Cycles) << "}";
1129 if (RAIdx + 1 < RAEnd)
1130 OS << ',';
1131 OS << " // #" << RAIdx << '\n';
1132 }
1133 OS << "}; // " << Target << "ReadAdvanceTable\n";
1134
1135 // Emit a SchedClass table for each processor.
1136 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1137 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1138 if (!PI->hasInstrSchedModel())
1139 continue;
1140
1141 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola322ff882012-11-02 20:57:36 +00001142 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Trick544c8802012-09-17 22:18:50 +00001143
1144 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1145 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1146 OS << "static const llvm::MCSchedClassDesc "
1147 << PI->ModelName << "SchedClasses[] = {\n";
1148
1149 // The first class is always invalid. We no way to distinguish it except by
1150 // name and position.
Andrew Trick1ab961f2013-03-16 18:58:55 +00001151 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Trick544c8802012-09-17 22:18:50 +00001152 && "invalid class not first");
1153 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1154 << MCSchedClassDesc::InvalidNumMicroOps
1155 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1156
1157 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1158 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1159 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1160 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1161 if (SchedClass.Name.size() < 18)
1162 OS.indent(18 - SchedClass.Name.size());
1163 OS << MCDesc.NumMicroOps
1164 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1165 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1166 << ", " << MCDesc.NumWriteProcResEntries
1167 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1168 << ", " << MCDesc.NumWriteLatencyEntries
1169 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1170 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1171 if (SCIdx + 1 < SCEnd)
1172 OS << ',';
1173 OS << " // #" << SCIdx << '\n';
1174 }
1175 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1176 }
1177}
1178
Andrew Trick2661b412012-07-07 04:00:00 +00001179void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1180 // For each processor model.
1181 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1182 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick40096d22012-09-17 22:18:45 +00001183 // Emit processor resource table.
1184 if (PI->hasInstrSchedModel())
1185 EmitProcessorResources(*PI, OS);
1186 else if(!PI->ProcResourceDefs.empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001187 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001188 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick40096d22012-09-17 22:18:45 +00001189
Andrew Trickfc992992012-06-05 03:44:40 +00001190 // Begin processor itinerary properties
1191 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001192 OS << "static const llvm::MCSchedModel " << PI->ModelName << "(\n";
1193 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
1194 EmitProcessorProp(OS, PI->ModelDef, "MinLatency", ',');
1195 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1196 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trick47579cf2013-01-09 03:36:49 +00001197 EmitProcessorProp(OS, PI->ModelDef, "ILPWindow", ',');
Andrew Trickd43b5c92012-08-08 02:44:16 +00001198 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Tricke127dfd2012-09-18 03:18:56 +00001199 OS << " " << PI->Index << ", // Processor ID\n";
1200 if (PI->hasInstrSchedModel())
1201 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1202 << " " << PI->ModelName << "SchedClasses" << ",\n"
1203 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1204 << " " << (SchedModels.schedClassEnd()
1205 - SchedModels.schedClassBegin()) << ",\n";
1206 else
1207 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
Andrew Trick1ab961f2013-03-16 18:58:55 +00001208 if (SchedModels.hasItineraries())
Andrew Trick40096d22012-09-17 22:18:45 +00001209 OS << " " << PI->ItinsDef->getName() << ");\n";
Andrew Trickd85934b2012-06-22 03:58:51 +00001210 else
Andrew Trick40096d22012-09-17 22:18:45 +00001211 OS << " 0); // No Itinerary\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001212 }
Jim Laskey10b1dd92005-10-31 17:16:01 +00001213}
1214
1215//
1216// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1217//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001218void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +00001219 // Gather and sort processor information
1220 std::vector<Record*> ProcessorList =
1221 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +00001222 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +00001223
1224 // Begin processor table
1225 OS << "\n";
1226 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001227 << "extern const llvm::SubtargetInfoKV "
Andrew Trick2661b412012-07-07 04:00:00 +00001228 << Target << "ProcSchedKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +00001229
Jim Laskey10b1dd92005-10-31 17:16:01 +00001230 // For each processor
1231 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1232 // Next processor
1233 Record *Processor = ProcessorList[i];
1234
Bill Wendling4222d802007-05-04 20:38:40 +00001235 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick2661b412012-07-07 04:00:00 +00001236 const std::string &ProcModelName =
Andrew Trick48605c32012-09-15 00:19:57 +00001237 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickda96cf22011-04-01 01:56:55 +00001238
Jim Laskey10b1dd92005-10-31 17:16:01 +00001239 // Emit as { "cpu", procinit },
Andrew Trick40096d22012-09-17 22:18:45 +00001240 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickda96cf22011-04-01 01:56:55 +00001241
Jim Laskey10b1dd92005-10-31 17:16:01 +00001242 // Depending on ''if more in the list'' emit comma
1243 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +00001244
Jim Laskey10b1dd92005-10-31 17:16:01 +00001245 OS << "\n";
1246 }
Andrew Trickda96cf22011-04-01 01:56:55 +00001247
Jim Laskey10b1dd92005-10-31 17:16:01 +00001248 // End processor table
1249 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001250}
1251
1252//
Andrew Trick2661b412012-07-07 04:00:00 +00001253// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey0d841e02005-10-27 19:47:21 +00001254//
Andrew Trick2661b412012-07-07 04:00:00 +00001255void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick40096d22012-09-17 22:18:45 +00001256 OS << "#ifdef DBGFIELD\n"
1257 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1258 << "#endif\n"
1259 << "#ifndef NDEBUG\n"
1260 << "#define DBGFIELD(x) x,\n"
1261 << "#else\n"
1262 << "#define DBGFIELD(x)\n"
1263 << "#endif\n";
1264
Andrew Trick1ab961f2013-03-16 18:58:55 +00001265 if (SchedModels.hasItineraries()) {
Andrew Trick2661b412012-07-07 04:00:00 +00001266 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey6cee6302005-11-01 20:06:59 +00001267 // Emit the stage data
Andrew Trick2661b412012-07-07 04:00:00 +00001268 EmitStageAndOperandCycleData(OS, ProcItinLists);
1269 EmitItineraries(OS, ProcItinLists);
Jim Laskey6cee6302005-11-01 20:06:59 +00001270 }
Andrew Trick544c8802012-09-17 22:18:50 +00001271 OS << "\n// ===============================================================\n"
1272 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick40096d22012-09-17 22:18:45 +00001273
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001274 SchedClassTables SchedTables;
1275 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1276 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1277 GenSchedClassTables(*PI, SchedTables);
1278 }
Andrew Trick544c8802012-09-17 22:18:50 +00001279 EmitSchedClassTables(SchedTables, OS);
1280
1281 // Emit the processor machine model
1282 EmitProcessorModels(OS);
1283 // Emit the processor lookup data
1284 EmitProcessorLookup(OS);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001285
Andrew Trick40096d22012-09-17 22:18:45 +00001286 OS << "#undef DBGFIELD";
Jim Laskey0d841e02005-10-27 19:47:21 +00001287}
1288
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001289void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1290 raw_ostream &OS) {
1291 OS << "unsigned " << ClassName
1292 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1293 << " const TargetSchedModel *SchedModel) const {\n";
1294
1295 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1296 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1297 for (std::vector<Record*>::const_iterator
1298 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1299 OS << (*PI)->getValueAsString("Code") << '\n';
1300 }
1301 IdxVec VariantClasses;
1302 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1303 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1304 if (SCI->Transitions.empty())
1305 continue;
Andrew Trick1ab961f2013-03-16 18:58:55 +00001306 VariantClasses.push_back(SCI->Index);
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001307 }
1308 if (!VariantClasses.empty()) {
1309 OS << " switch (SchedClass) {\n";
1310 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1311 VCI != VCE; ++VCI) {
1312 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1313 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1314 IdxVec ProcIndices;
1315 for (std::vector<CodeGenSchedTransition>::const_iterator
1316 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1317 TI != TE; ++TI) {
1318 IdxVec PI;
1319 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1320 ProcIndices.begin(), ProcIndices.end(),
1321 std::back_inserter(PI));
1322 ProcIndices.swap(PI);
1323 }
1324 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1325 PI != PE; ++PI) {
1326 OS << " ";
1327 if (*PI != 0)
1328 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1329 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1330 << '\n';
1331 for (std::vector<CodeGenSchedTransition>::const_iterator
1332 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1333 TI != TE; ++TI) {
1334 OS << " if (";
1335 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1336 TI->ProcIndices.end(), *PI)) {
1337 continue;
1338 }
1339 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1340 RI != RE; ++RI) {
1341 if (RI != TI->PredTerm.begin())
1342 OS << "\n && ";
1343 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1344 }
1345 OS << ")\n"
1346 << " return " << TI->ToClassIdx << "; // "
1347 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1348 }
1349 OS << " }\n";
1350 if (*PI == 0)
1351 break;
1352 }
Andrew Trick1ab961f2013-03-16 18:58:55 +00001353 if (SC.isInferred())
1354 OS << " return " << SC.Index << ";\n";
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001355 OS << " break;\n";
1356 }
1357 OS << " };\n";
1358 }
1359 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1360 << "} // " << ClassName << "::resolveSchedClass\n";
1361}
1362
Jim Laskey0d841e02005-10-27 19:47:21 +00001363//
Jim Laskey581a8f72005-10-26 17:30:34 +00001364// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1365// the subtarget features string.
1366//
Evan Cheng94214702011-07-01 20:45:01 +00001367void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1368 unsigned NumFeatures,
1369 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001370 std::vector<Record*> Features =
1371 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina42d24c72005-12-30 14:56:37 +00001372 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +00001373
Andrew Trickda96cf22011-04-01 01:56:55 +00001374 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1375 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +00001376 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +00001377 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001378 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenef0fd3af2010-01-05 17:47:41 +00001379 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel3f696e52012-06-12 04:21:36 +00001380 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng94214702011-07-01 20:45:01 +00001381
1382 if (Features.empty()) {
1383 OS << "}\n";
1384 return;
1385 }
1386
Andrew Trick34aadd62012-09-18 05:33:15 +00001387 OS << " InitMCProcessorInfo(CPU, FS);\n"
1388 << " uint64_t Bits = getFeatureBits();\n";
Bill Wendling4222d802007-05-04 20:38:40 +00001389
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001390 for (unsigned i = 0; i < Features.size(); i++) {
1391 // Next record
1392 Record *R = Features[i];
Bill Wendling4222d802007-05-04 20:38:40 +00001393 const std::string &Instance = R->getName();
1394 const std::string &Value = R->getValueAsString("Value");
1395 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +00001396
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001397 if (Value=="true" || Value=="false")
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001398 OS << " if ((Bits & " << Target << "::"
1399 << Instance << ") != 0) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001400 << Attribute << " = " << Value << ";\n";
1401 else
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001402 OS << " if ((Bits & " << Target << "::"
1403 << Instance << ") != 0 && "
Evan Cheng94214702011-07-01 20:45:01 +00001404 << Attribute << " < " << Value << ") "
1405 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +00001406 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001407
Evan Cheng276365d2011-06-30 01:53:36 +00001408 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +00001409}
1410
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001411//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001412// SubtargetEmitter::run - Main subtarget enumeration emitter.
1413//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001414void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001415 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001416
Evan Chengebdeeab2011-07-08 01:53:10 +00001417 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1418 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1419
1420 OS << "namespace llvm {\n";
1421 Enumeration(OS, "SubtargetFeature", true);
1422 OS << "} // End llvm namespace \n";
1423 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1424
Evan Cheng94214702011-07-01 20:45:01 +00001425 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1426 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +00001427
Evan Cheng94214702011-07-01 20:45:01 +00001428 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001429#if 0
1430 OS << "namespace {\n";
1431#endif
Evan Cheng94214702011-07-01 20:45:01 +00001432 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001433 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +00001434 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001435 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001436 EmitSchedModel(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001437 OS << "\n";
1438#if 0
1439 OS << "}\n";
1440#endif
Evan Cheng94214702011-07-01 20:45:01 +00001441
1442 // MCInstrInfo initialization routine.
1443 OS << "static inline void Init" << Target
Evan Cheng59ee62d2011-07-11 03:57:24 +00001444 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1445 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1446 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001447 if (NumFeatures)
1448 OS << Target << "FeatureKV, ";
1449 else
1450 OS << "0, ";
1451 if (NumProcs)
1452 OS << Target << "SubTypeKV, ";
1453 else
1454 OS << "0, ";
Andrew Trick544c8802012-09-17 22:18:50 +00001455 OS << '\n'; OS.indent(22);
Andrew Tricke127dfd2012-09-18 03:18:56 +00001456 OS << Target << "ProcSchedKV, "
1457 << Target << "WriteProcResTable, "
1458 << Target << "WriteLatencyTable, "
1459 << Target << "ReadAdvanceTable, ";
Andrew Trick1ab961f2013-03-16 18:58:55 +00001460 if (SchedModels.hasItineraries()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001461 OS << '\n'; OS.indent(22);
1462 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001463 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001464 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001465 } else
Andrew Tricke127dfd2012-09-18 03:18:56 +00001466 OS << "0, 0, 0, ";
Evan Cheng94214702011-07-01 20:45:01 +00001467 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
1468
1469 OS << "} // End llvm namespace \n";
1470
1471 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1472
1473 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1474 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1475
1476 OS << "#include \"llvm/Support/Debug.h\"\n";
1477 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
1478 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1479
1480 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1481
Evan Cheng5b1b44892011-07-01 21:01:15 +00001482 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +00001483 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1484 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1485
1486 std::string ClassName = Target + "GenSubtargetInfo";
1487 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001488 OS << "class DFAPacketizer;\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +00001489 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001490 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1491 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001492 << "public:\n"
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001493 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
1494 << " const TargetSchedModel *SchedModel) const;\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +00001495 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001496 << " const;\n"
Evan Cheng94214702011-07-01 20:45:01 +00001497 << "};\n";
1498 OS << "} // End llvm namespace \n";
1499
1500 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1501
1502 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1503 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1504
Andrew Trickee290ba2012-09-18 03:32:57 +00001505 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng94214702011-07-01 20:45:01 +00001506 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001507 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1508 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001509 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1510 OS << "extern const llvm::MCWriteProcResEntry "
1511 << Target << "WriteProcResTable[];\n";
1512 OS << "extern const llvm::MCWriteLatencyEntry "
1513 << Target << "WriteLatencyTable[];\n";
1514 OS << "extern const llvm::MCReadAdvanceEntry "
1515 << Target << "ReadAdvanceTable[];\n";
1516
Andrew Trick1ab961f2013-03-16 18:58:55 +00001517 if (SchedModels.hasItineraries()) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001518 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1519 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Tricka11a6282012-07-07 03:59:48 +00001520 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001521 }
1522
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001523 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1524 << "StringRef FS)\n"
Evan Cheng5b1b44892011-07-01 21:01:15 +00001525 << " : TargetSubtargetInfo() {\n"
Evan Cheng59ee62d2011-07-11 03:57:24 +00001526 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001527 if (NumFeatures)
1528 OS << Target << "FeatureKV, ";
1529 else
1530 OS << "0, ";
1531 if (NumProcs)
1532 OS << Target << "SubTypeKV, ";
1533 else
1534 OS << "0, ";
Andrew Tricke127dfd2012-09-18 03:18:56 +00001535 OS << '\n'; OS.indent(22);
1536 OS << Target << "ProcSchedKV, "
1537 << Target << "WriteProcResTable, "
1538 << Target << "WriteLatencyTable, "
1539 << Target << "ReadAdvanceTable, ";
1540 OS << '\n'; OS.indent(22);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001541 if (SchedModels.hasItineraries()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001542 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001543 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001544 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001545 } else
Andrew Tricke127dfd2012-09-18 03:18:56 +00001546 OS << "0, 0, 0, ";
Evan Cheng94214702011-07-01 20:45:01 +00001547 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001548
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001549 EmitSchedModelHelpers(ClassName, OS);
1550
Evan Cheng94214702011-07-01 20:45:01 +00001551 OS << "} // End llvm namespace \n";
1552
1553 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001554}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001555
1556namespace llvm {
1557
1558void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick2661b412012-07-07 04:00:00 +00001559 CodeGenTarget CGTarget(RK);
1560 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001561}
1562
1563} // End llvm namespace