blob: 4918b1b143ec5b8269837caa985e5b6578b10a7d [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
Andrew Tricka809c8d2013-04-23 23:45:16 +0000785// resource groups and super resources that cover them.
Andrew Trick1754aca2013-03-14 21:21:50 +0000786void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
787 std::vector<int64_t> &Cycles,
Andrew Tricka809c8d2013-04-23 23:45:16 +0000788 const CodeGenProcModel &PM) {
Andrew Trick1754aca2013-03-14 21:21:50 +0000789 // Default to 1 resource cycle.
790 Cycles.resize(PRVec.size(), 1);
791 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
Andrew Tricka809c8d2013-04-23 23:45:16 +0000792 Record *PRDef = PRVec[i];
Andrew Trick1754aca2013-03-14 21:21:50 +0000793 RecVec SubResources;
Andrew Tricka809c8d2013-04-23 23:45:16 +0000794 if (PRDef->isSubClassOf("ProcResGroup"))
795 SubResources = PRDef->getValueAsListOfDefs("Resources");
Andrew Trick1754aca2013-03-14 21:21:50 +0000796 else {
Andrew Tricka809c8d2013-04-23 23:45:16 +0000797 SubResources.push_back(PRDef);
798 PRDef = SchedModels.findProcResUnits(PRVec[i], PM);
799 for (Record *SubDef = PRDef;
800 SubDef->getValueInit("Super")->isComplete();) {
801 if (SubDef->isSubClassOf("ProcResGroup")) {
802 // Disallow this for simplicitly.
803 PrintFatalError(SubDef->getLoc(), "Processor resource group "
804 " cannot be a super resources.");
805 }
806 Record *SuperDef =
807 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM);
808 PRVec.push_back(SuperDef);
809 Cycles.push_back(Cycles[i]);
810 SubDef = SuperDef;
811 }
Andrew Trick1754aca2013-03-14 21:21:50 +0000812 }
Andrew Tricka809c8d2013-04-23 23:45:16 +0000813 for (RecIter PRI = PM.ProcResourceDefs.begin(),
814 PRE = PM.ProcResourceDefs.end();
Andrew Trick1754aca2013-03-14 21:21:50 +0000815 PRI != PRE; ++PRI) {
Andrew Tricka809c8d2013-04-23 23:45:16 +0000816 if (*PRI == PRDef || !(*PRI)->isSubClassOf("ProcResGroup"))
Andrew Trick1754aca2013-03-14 21:21:50 +0000817 continue;
818 RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources");
Andrew Trick1754aca2013-03-14 21:21:50 +0000819 RecIter SubI = SubResources.begin(), SubE = SubResources.end();
Andrew Trick6982bdd2013-04-23 23:45:11 +0000820 for( ; SubI != SubE; ++SubI) {
821 if (std::find(SuperResources.begin(), SuperResources.end(), *SubI)
822 == SuperResources.end()) {
Andrew Trick1754aca2013-03-14 21:21:50 +0000823 break;
Andrew Trick6982bdd2013-04-23 23:45:11 +0000824 }
Andrew Trick1754aca2013-03-14 21:21:50 +0000825 }
826 if (SubI == SubE) {
827 PRVec.push_back(*PRI);
828 Cycles.push_back(Cycles[i]);
829 }
830 }
831 }
832}
833
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000834// Generate the SchedClass table for this processor and update global
835// tables. Must be called for each processor in order.
836void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
837 SchedClassTables &SchedTables) {
838 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
839 if (!ProcModel.hasInstrSchedModel())
840 return;
841
842 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
843 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
844 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
Andrew Trickfe05d982012-10-03 23:06:25 +0000845 DEBUG(SCI->dump(&SchedModels));
846
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000847 SCTab.resize(SCTab.size() + 1);
848 MCSchedClassDesc &SCDesc = SCTab.back();
Andrew Tricke127dfd2012-09-18 03:18:56 +0000849 // SCDesc.Name is guarded by NDEBUG
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000850 SCDesc.NumMicroOps = 0;
851 SCDesc.BeginGroup = false;
852 SCDesc.EndGroup = false;
853 SCDesc.WriteProcResIdx = 0;
854 SCDesc.WriteLatencyIdx = 0;
855 SCDesc.ReadAdvanceIdx = 0;
856
857 // A Variant SchedClass has no resources of its own.
Andrew Trick82e7c4f2013-03-26 21:36:39 +0000858 bool HasVariants = false;
859 for (std::vector<CodeGenSchedTransition>::const_iterator
860 TI = SCI->Transitions.begin(), TE = SCI->Transitions.end();
861 TI != TE; ++TI) {
862 if (TI->ProcIndices[0] == 0) {
863 HasVariants = true;
864 break;
865 }
866 IdxIter PIPos = std::find(TI->ProcIndices.begin(),
867 TI->ProcIndices.end(), ProcModel.Index);
868 if (PIPos != TI->ProcIndices.end()) {
869 HasVariants = true;
870 break;
871 }
872 }
873 if (HasVariants) {
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000874 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
875 continue;
876 }
877
878 // Determine if the SchedClass is actually reachable on this processor. If
879 // not don't try to locate the processor resources, it will fail.
880 // If ProcIndices contains 0, this class applies to all processors.
881 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
882 if (SCI->ProcIndices[0] != 0) {
883 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
884 SCI->ProcIndices.end(), ProcModel.Index);
885 if (PIPos == SCI->ProcIndices.end())
886 continue;
887 }
888 IdxVec Writes = SCI->Writes;
889 IdxVec Reads = SCI->Reads;
Andrew Trick1ab961f2013-03-16 18:58:55 +0000890 if (!SCI->InstRWs.empty()) {
891 // This class has a default ReadWrite list which can be overriden by
Andrew Trickfe05d982012-10-03 23:06:25 +0000892 // InstRW definitions.
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000893 Record *RWDef = 0;
894 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
895 RWI != RWE; ++RWI) {
896 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
897 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
898 RWDef = *RWI;
899 break;
900 }
901 }
902 if (RWDef) {
Andrew Trick2062b122012-10-03 23:06:28 +0000903 Writes.clear();
904 Reads.clear();
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000905 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
906 Writes, Reads);
907 }
908 }
Andrew Trick1ab961f2013-03-16 18:58:55 +0000909 if (Writes.empty()) {
910 // Check this processor's itinerary class resources.
911 for (RecIter II = ProcModel.ItinRWDefs.begin(),
912 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
913 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
914 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
915 != Matched.end()) {
916 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
917 Writes, Reads);
918 break;
919 }
920 }
921 if (Writes.empty()) {
922 DEBUG(dbgs() << ProcModel.ModelName
923 << " does not have resources for class " << SCI->Name << '\n');
924 }
925 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000926 // Sum resources across all operand writes.
927 std::vector<MCWriteProcResEntry> WriteProcResources;
928 std::vector<MCWriteLatencyEntry> WriteLatencies;
Andrew Trick3b8fb642012-09-19 04:43:19 +0000929 std::vector<std::string> WriterNames;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000930 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
931 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
932 IdxVec WriteSeq;
Andrew Trick2062b122012-10-03 23:06:28 +0000933 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
934 ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000935
936 // For each operand, create a latency entry.
937 MCWriteLatencyEntry WLEntry;
938 WLEntry.Cycles = 0;
Andrew Trick3b8fb642012-09-19 04:43:19 +0000939 unsigned WriteID = WriteSeq.back();
940 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
941 // If this Write is not referenced by a ReadAdvance, don't distinguish it
942 // from other WriteLatency entries.
Andrew Trick1ab961f2013-03-16 18:58:55 +0000943 if (!SchedModels.hasReadOfWrite(
944 SchedModels.getSchedWrite(WriteID).TheDef)) {
Andrew Trick3b8fb642012-09-19 04:43:19 +0000945 WriteID = 0;
946 }
947 WLEntry.WriteResourceID = WriteID;
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000948
949 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
950 WSI != WSE; ++WSI) {
951
Andrew Trick92649882012-09-22 02:24:21 +0000952 Record *WriteRes =
953 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000954
955 // Mark the parent class as invalid for unsupported write types.
956 if (WriteRes->getValueAsBit("Unsupported")) {
957 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
958 break;
959 }
960 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
961 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
962 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
963 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
964
965 // Create an entry for each ProcResource listed in WriteRes.
966 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
967 std::vector<int64_t> Cycles =
968 WriteRes->getValueAsListOfInts("ResourceCycles");
Andrew Trick1754aca2013-03-14 21:21:50 +0000969
970 ExpandProcResources(PRVec, Cycles, ProcModel);
971
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000972 for (unsigned PRIdx = 0, PREnd = PRVec.size();
973 PRIdx != PREnd; ++PRIdx) {
974 MCWriteProcResEntry WPREntry;
975 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
976 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
Andrew Trick1754aca2013-03-14 21:21:50 +0000977 WPREntry.Cycles = Cycles[PRIdx];
Andrew Trickc8121102013-03-01 23:31:26 +0000978 // If this resource is already used in this sequence, add the current
979 // entry's cycles so that the same resource appears to be used
980 // serially, rather than multiple parallel uses. This is important for
981 // in-order machine where the resource consumption is a hazard.
982 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
983 for( ; WPRIdx != WPREnd; ++WPRIdx) {
984 if (WriteProcResources[WPRIdx].ProcResourceIdx
985 == WPREntry.ProcResourceIdx) {
986 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
987 break;
988 }
989 }
990 if (WPRIdx == WPREnd)
991 WriteProcResources.push_back(WPREntry);
Andrew Trick52c3a1d2012-09-17 22:18:48 +0000992 }
993 }
994 WriteLatencies.push_back(WLEntry);
995 }
996 // Create an entry for each operand Read in this SchedClass.
997 // Entries must be sorted first by UseIdx then by WriteResourceID.
998 for (unsigned UseIdx = 0, EndIdx = Reads.size();
999 UseIdx != EndIdx; ++UseIdx) {
Andrew Trick92649882012-09-22 02:24:21 +00001000 Record *ReadAdvance =
1001 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001002 if (!ReadAdvance)
1003 continue;
1004
1005 // Mark the parent class as invalid for unsupported write types.
1006 if (ReadAdvance->getValueAsBit("Unsupported")) {
1007 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1008 break;
1009 }
1010 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1011 IdxVec WriteIDs;
1012 if (ValidWrites.empty())
1013 WriteIDs.push_back(0);
1014 else {
1015 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
1016 VWI != VWE; ++VWI) {
1017 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
1018 }
1019 }
1020 std::sort(WriteIDs.begin(), WriteIDs.end());
1021 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
1022 MCReadAdvanceEntry RAEntry;
1023 RAEntry.UseIdx = UseIdx;
1024 RAEntry.WriteResourceID = *WI;
1025 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1026 ReadAdvanceEntries.push_back(RAEntry);
1027 }
1028 }
1029 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1030 WriteProcResources.clear();
1031 WriteLatencies.clear();
1032 ReadAdvanceEntries.clear();
1033 }
1034 // Add the information for this SchedClass to the global tables using basic
1035 // compression.
1036 //
1037 // WritePrecRes entries are sorted by ProcResIdx.
1038 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
1039 LessWriteProcResources());
1040
1041 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1042 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1043 std::search(SchedTables.WriteProcResources.begin(),
1044 SchedTables.WriteProcResources.end(),
1045 WriteProcResources.begin(), WriteProcResources.end());
1046 if (WPRPos != SchedTables.WriteProcResources.end())
1047 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1048 else {
1049 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1050 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1051 WriteProcResources.end());
1052 }
1053 // Latency entries must remain in operand order.
1054 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1055 std::vector<MCWriteLatencyEntry>::iterator WLPos =
1056 std::search(SchedTables.WriteLatencies.begin(),
1057 SchedTables.WriteLatencies.end(),
1058 WriteLatencies.begin(), WriteLatencies.end());
Andrew Trick3b8fb642012-09-19 04:43:19 +00001059 if (WLPos != SchedTables.WriteLatencies.end()) {
1060 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1061 SCDesc.WriteLatencyIdx = idx;
1062 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1063 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1064 std::string::npos) {
1065 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1066 }
1067 }
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001068 else {
1069 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
Andrew Trick3b8fb642012-09-19 04:43:19 +00001070 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
1071 WriteLatencies.begin(),
1072 WriteLatencies.end());
1073 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
1074 WriterNames.begin(), WriterNames.end());
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001075 }
1076 // ReadAdvanceEntries must remain in operand order.
1077 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1078 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1079 std::search(SchedTables.ReadAdvanceEntries.begin(),
1080 SchedTables.ReadAdvanceEntries.end(),
1081 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1082 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1083 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1084 else {
1085 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1086 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
1087 ReadAdvanceEntries.end());
1088 }
1089 }
1090}
1091
Andrew Trick544c8802012-09-17 22:18:50 +00001092// Emit SchedClass tables for all processors and associated global tables.
1093void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1094 raw_ostream &OS) {
1095 // Emit global WriteProcResTable.
1096 OS << "\n// {ProcResourceIdx, Cycles}\n"
1097 << "extern const llvm::MCWriteProcResEntry "
1098 << Target << "WriteProcResTable[] = {\n"
1099 << " { 0, 0}, // Invalid\n";
1100 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1101 WPRIdx != WPREnd; ++WPRIdx) {
1102 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1103 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1104 << format("%2d", WPREntry.Cycles) << "}";
1105 if (WPRIdx + 1 < WPREnd)
1106 OS << ',';
1107 OS << " // #" << WPRIdx << '\n';
1108 }
1109 OS << "}; // " << Target << "WriteProcResTable\n";
1110
1111 // Emit global WriteLatencyTable.
1112 OS << "\n// {Cycles, WriteResourceID}\n"
1113 << "extern const llvm::MCWriteLatencyEntry "
1114 << Target << "WriteLatencyTable[] = {\n"
1115 << " { 0, 0}, // Invalid\n";
1116 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1117 WLIdx != WLEnd; ++WLIdx) {
1118 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1119 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1120 << format("%2d", WLEntry.WriteResourceID) << "}";
1121 if (WLIdx + 1 < WLEnd)
1122 OS << ',';
Andrew Trick3b8fb642012-09-19 04:43:19 +00001123 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
Andrew Trick544c8802012-09-17 22:18:50 +00001124 }
1125 OS << "}; // " << Target << "WriteLatencyTable\n";
1126
1127 // Emit global ReadAdvanceTable.
1128 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1129 << "extern const llvm::MCReadAdvanceEntry "
1130 << Target << "ReadAdvanceTable[] = {\n"
1131 << " {0, 0, 0}, // Invalid\n";
1132 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1133 RAIdx != RAEnd; ++RAIdx) {
1134 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1135 OS << " {" << RAEntry.UseIdx << ", "
1136 << format("%2d", RAEntry.WriteResourceID) << ", "
1137 << format("%2d", RAEntry.Cycles) << "}";
1138 if (RAIdx + 1 < RAEnd)
1139 OS << ',';
1140 OS << " // #" << RAIdx << '\n';
1141 }
1142 OS << "}; // " << Target << "ReadAdvanceTable\n";
1143
1144 // Emit a SchedClass table for each processor.
1145 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1146 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1147 if (!PI->hasInstrSchedModel())
1148 continue;
1149
1150 std::vector<MCSchedClassDesc> &SCTab =
Rafael Espindola322ff882012-11-02 20:57:36 +00001151 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
Andrew Trick544c8802012-09-17 22:18:50 +00001152
1153 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1154 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1155 OS << "static const llvm::MCSchedClassDesc "
1156 << PI->ModelName << "SchedClasses[] = {\n";
1157
1158 // The first class is always invalid. We no way to distinguish it except by
1159 // name and position.
Andrew Trick1ab961f2013-03-16 18:58:55 +00001160 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
Andrew Trick544c8802012-09-17 22:18:50 +00001161 && "invalid class not first");
1162 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1163 << MCSchedClassDesc::InvalidNumMicroOps
1164 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1165
1166 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1167 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1168 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1169 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1170 if (SchedClass.Name.size() < 18)
1171 OS.indent(18 - SchedClass.Name.size());
1172 OS << MCDesc.NumMicroOps
1173 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1174 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1175 << ", " << MCDesc.NumWriteProcResEntries
1176 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1177 << ", " << MCDesc.NumWriteLatencyEntries
1178 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1179 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1180 if (SCIdx + 1 < SCEnd)
1181 OS << ',';
1182 OS << " // #" << SCIdx << '\n';
1183 }
1184 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1185 }
1186}
1187
Andrew Trick2661b412012-07-07 04:00:00 +00001188void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1189 // For each processor model.
1190 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1191 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
Andrew Trick40096d22012-09-17 22:18:45 +00001192 // Emit processor resource table.
1193 if (PI->hasInstrSchedModel())
1194 EmitProcessorResources(*PI, OS);
1195 else if(!PI->ProcResourceDefs.empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001196 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001197 "ProcResources without defining WriteRes SchedWriteRes");
Andrew Trick40096d22012-09-17 22:18:45 +00001198
Andrew Trickfc992992012-06-05 03:44:40 +00001199 // Begin processor itinerary properties
1200 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001201 OS << "static const llvm::MCSchedModel " << PI->ModelName << "(\n";
1202 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
1203 EmitProcessorProp(OS, PI->ModelDef, "MinLatency", ',');
1204 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1205 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
Andrew Trick47579cf2013-01-09 03:36:49 +00001206 EmitProcessorProp(OS, PI->ModelDef, "ILPWindow", ',');
Andrew Trickd43b5c92012-08-08 02:44:16 +00001207 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
Andrew Tricke127dfd2012-09-18 03:18:56 +00001208 OS << " " << PI->Index << ", // Processor ID\n";
1209 if (PI->hasInstrSchedModel())
1210 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1211 << " " << PI->ModelName << "SchedClasses" << ",\n"
1212 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1213 << " " << (SchedModels.schedClassEnd()
1214 - SchedModels.schedClassBegin()) << ",\n";
1215 else
1216 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
Andrew Trick1ab961f2013-03-16 18:58:55 +00001217 if (SchedModels.hasItineraries())
Andrew Trick40096d22012-09-17 22:18:45 +00001218 OS << " " << PI->ItinsDef->getName() << ");\n";
Andrew Trickd85934b2012-06-22 03:58:51 +00001219 else
Andrew Trick40096d22012-09-17 22:18:45 +00001220 OS << " 0); // No Itinerary\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001221 }
Jim Laskey10b1dd92005-10-31 17:16:01 +00001222}
1223
1224//
1225// EmitProcessorLookup - generate cpu name to itinerary lookup table.
1226//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001227void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Jim Laskey10b1dd92005-10-31 17:16:01 +00001228 // Gather and sort processor information
1229 std::vector<Record*> ProcessorList =
1230 Records.getAllDerivedDefinitions("Processor");
Duraid Madina42d24c72005-12-30 14:56:37 +00001231 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
Jim Laskey10b1dd92005-10-31 17:16:01 +00001232
1233 // Begin processor table
1234 OS << "\n";
1235 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001236 << "extern const llvm::SubtargetInfoKV "
Andrew Trick2661b412012-07-07 04:00:00 +00001237 << Target << "ProcSchedKV[] = {\n";
Andrew Trickda96cf22011-04-01 01:56:55 +00001238
Jim Laskey10b1dd92005-10-31 17:16:01 +00001239 // For each processor
1240 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1241 // Next processor
1242 Record *Processor = ProcessorList[i];
1243
Bill Wendling4222d802007-05-04 20:38:40 +00001244 const std::string &Name = Processor->getValueAsString("Name");
Andrew Trick2661b412012-07-07 04:00:00 +00001245 const std::string &ProcModelName =
Andrew Trick48605c32012-09-15 00:19:57 +00001246 SchedModels.getModelForProc(Processor).ModelName;
Andrew Trickda96cf22011-04-01 01:56:55 +00001247
Jim Laskey10b1dd92005-10-31 17:16:01 +00001248 // Emit as { "cpu", procinit },
Andrew Trick40096d22012-09-17 22:18:45 +00001249 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
Andrew Trickda96cf22011-04-01 01:56:55 +00001250
Jim Laskey10b1dd92005-10-31 17:16:01 +00001251 // Depending on ''if more in the list'' emit comma
1252 if (++i < N) OS << ",";
Andrew Trickda96cf22011-04-01 01:56:55 +00001253
Jim Laskey10b1dd92005-10-31 17:16:01 +00001254 OS << "\n";
1255 }
Andrew Trickda96cf22011-04-01 01:56:55 +00001256
Jim Laskey10b1dd92005-10-31 17:16:01 +00001257 // End processor table
1258 OS << "};\n";
Jim Laskey0d841e02005-10-27 19:47:21 +00001259}
1260
1261//
Andrew Trick2661b412012-07-07 04:00:00 +00001262// EmitSchedModel - Emits all scheduling model tables, folding common patterns.
Jim Laskey0d841e02005-10-27 19:47:21 +00001263//
Andrew Trick2661b412012-07-07 04:00:00 +00001264void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
Andrew Trick40096d22012-09-17 22:18:45 +00001265 OS << "#ifdef DBGFIELD\n"
1266 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1267 << "#endif\n"
1268 << "#ifndef NDEBUG\n"
1269 << "#define DBGFIELD(x) x,\n"
1270 << "#else\n"
1271 << "#define DBGFIELD(x)\n"
1272 << "#endif\n";
1273
Andrew Trick1ab961f2013-03-16 18:58:55 +00001274 if (SchedModels.hasItineraries()) {
Andrew Trick2661b412012-07-07 04:00:00 +00001275 std::vector<std::vector<InstrItinerary> > ProcItinLists;
Jim Laskey6cee6302005-11-01 20:06:59 +00001276 // Emit the stage data
Andrew Trick2661b412012-07-07 04:00:00 +00001277 EmitStageAndOperandCycleData(OS, ProcItinLists);
1278 EmitItineraries(OS, ProcItinLists);
Jim Laskey6cee6302005-11-01 20:06:59 +00001279 }
Andrew Trick544c8802012-09-17 22:18:50 +00001280 OS << "\n// ===============================================================\n"
1281 << "// Data tables for the new per-operand machine model.\n";
Andrew Trick40096d22012-09-17 22:18:45 +00001282
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001283 SchedClassTables SchedTables;
1284 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1285 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1286 GenSchedClassTables(*PI, SchedTables);
1287 }
Andrew Trick544c8802012-09-17 22:18:50 +00001288 EmitSchedClassTables(SchedTables, OS);
1289
1290 // Emit the processor machine model
1291 EmitProcessorModels(OS);
1292 // Emit the processor lookup data
1293 EmitProcessorLookup(OS);
Andrew Trick52c3a1d2012-09-17 22:18:48 +00001294
Andrew Trick40096d22012-09-17 22:18:45 +00001295 OS << "#undef DBGFIELD";
Jim Laskey0d841e02005-10-27 19:47:21 +00001296}
1297
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001298void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1299 raw_ostream &OS) {
1300 OS << "unsigned " << ClassName
1301 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1302 << " const TargetSchedModel *SchedModel) const {\n";
1303
1304 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1305 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1306 for (std::vector<Record*>::const_iterator
1307 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1308 OS << (*PI)->getValueAsString("Code") << '\n';
1309 }
1310 IdxVec VariantClasses;
1311 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1312 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1313 if (SCI->Transitions.empty())
1314 continue;
Andrew Trick1ab961f2013-03-16 18:58:55 +00001315 VariantClasses.push_back(SCI->Index);
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001316 }
1317 if (!VariantClasses.empty()) {
1318 OS << " switch (SchedClass) {\n";
1319 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1320 VCI != VCE; ++VCI) {
1321 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1322 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1323 IdxVec ProcIndices;
1324 for (std::vector<CodeGenSchedTransition>::const_iterator
1325 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1326 TI != TE; ++TI) {
1327 IdxVec PI;
1328 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1329 ProcIndices.begin(), ProcIndices.end(),
1330 std::back_inserter(PI));
1331 ProcIndices.swap(PI);
1332 }
1333 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1334 PI != PE; ++PI) {
1335 OS << " ";
1336 if (*PI != 0)
1337 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1338 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1339 << '\n';
1340 for (std::vector<CodeGenSchedTransition>::const_iterator
1341 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1342 TI != TE; ++TI) {
Arnold Schwaighofer8a227082013-06-04 22:35:17 +00001343 OS << " if (";
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001344 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1345 TI->ProcIndices.end(), *PI)) {
1346 continue;
1347 }
1348 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1349 RI != RE; ++RI) {
1350 if (RI != TI->PredTerm.begin())
1351 OS << "\n && ";
1352 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1353 }
1354 OS << ")\n"
1355 << " return " << TI->ToClassIdx << "; // "
1356 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1357 }
1358 OS << " }\n";
1359 if (*PI == 0)
1360 break;
1361 }
Andrew Trick1ab961f2013-03-16 18:58:55 +00001362 if (SC.isInferred())
1363 OS << " return " << SC.Index << ";\n";
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001364 OS << " break;\n";
1365 }
1366 OS << " };\n";
1367 }
1368 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1369 << "} // " << ClassName << "::resolveSchedClass\n";
1370}
1371
Jim Laskey0d841e02005-10-27 19:47:21 +00001372//
Jim Laskey581a8f72005-10-26 17:30:34 +00001373// ParseFeaturesFunction - Produces a subtarget specific function for parsing
1374// the subtarget features string.
1375//
Evan Cheng94214702011-07-01 20:45:01 +00001376void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1377 unsigned NumFeatures,
1378 unsigned NumProcs) {
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001379 std::vector<Record*> Features =
1380 Records.getAllDerivedDefinitions("SubtargetFeature");
Duraid Madina42d24c72005-12-30 14:56:37 +00001381 std::sort(Features.begin(), Features.end(), LessRecord());
Jim Laskey581a8f72005-10-26 17:30:34 +00001382
Andrew Trickda96cf22011-04-01 01:56:55 +00001383 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1384 << "// subtarget options.\n"
Evan Cheng276365d2011-06-30 01:53:36 +00001385 << "void llvm::";
Jim Laskey581a8f72005-10-26 17:30:34 +00001386 OS << Target;
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001387 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
David Greenef0fd3af2010-01-05 17:47:41 +00001388 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
Hal Finkel3f696e52012-06-12 04:21:36 +00001389 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
Evan Cheng94214702011-07-01 20:45:01 +00001390
1391 if (Features.empty()) {
1392 OS << "}\n";
1393 return;
1394 }
1395
Andrew Trick34aadd62012-09-18 05:33:15 +00001396 OS << " InitMCProcessorInfo(CPU, FS);\n"
1397 << " uint64_t Bits = getFeatureBits();\n";
Bill Wendling4222d802007-05-04 20:38:40 +00001398
Jim Laskeyf7bcde02005-10-28 21:47:29 +00001399 for (unsigned i = 0; i < Features.size(); i++) {
1400 // Next record
1401 Record *R = Features[i];
Bill Wendling4222d802007-05-04 20:38:40 +00001402 const std::string &Instance = R->getName();
1403 const std::string &Value = R->getValueAsString("Value");
1404 const std::string &Attribute = R->getValueAsString("Attribute");
Evan Cheng19c95502006-01-27 08:09:42 +00001405
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001406 if (Value=="true" || Value=="false")
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001407 OS << " if ((Bits & " << Target << "::"
1408 << Instance << ") != 0) "
Dale Johannesendb01c8b2008-02-14 23:35:16 +00001409 << Attribute << " = " << Value << ";\n";
1410 else
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001411 OS << " if ((Bits & " << Target << "::"
1412 << Instance << ") != 0 && "
Evan Cheng94214702011-07-01 20:45:01 +00001413 << Attribute << " < " << Value << ") "
1414 << Attribute << " = " << Value << ";\n";
Jim Laskey6cee6302005-11-01 20:06:59 +00001415 }
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001416
Evan Cheng276365d2011-06-30 01:53:36 +00001417 OS << "}\n";
Jim Laskey581a8f72005-10-26 17:30:34 +00001418}
1419
Anton Korobeynikov41a02432009-05-23 19:50:50 +00001420//
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001421// SubtargetEmitter::run - Main subtarget enumeration emitter.
1422//
Daniel Dunbar1a551802009-07-03 00:10:29 +00001423void SubtargetEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001424 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001425
Evan Chengebdeeab2011-07-08 01:53:10 +00001426 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1427 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1428
1429 OS << "namespace llvm {\n";
1430 Enumeration(OS, "SubtargetFeature", true);
1431 OS << "} // End llvm namespace \n";
1432 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1433
Evan Cheng94214702011-07-01 20:45:01 +00001434 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1435 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
Anton Korobeynikov928eb492010-04-18 20:31:01 +00001436
Evan Cheng94214702011-07-01 20:45:01 +00001437 OS << "namespace llvm {\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001438#if 0
1439 OS << "namespace {\n";
1440#endif
Evan Cheng94214702011-07-01 20:45:01 +00001441 unsigned NumFeatures = FeatureKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001442 OS << "\n";
Evan Cheng94214702011-07-01 20:45:01 +00001443 unsigned NumProcs = CPUKeyValues(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001444 OS << "\n";
Andrew Trick2661b412012-07-07 04:00:00 +00001445 EmitSchedModel(OS);
Evan Chengc60f9b72011-07-14 20:59:42 +00001446 OS << "\n";
1447#if 0
1448 OS << "}\n";
1449#endif
Evan Cheng94214702011-07-01 20:45:01 +00001450
1451 // MCInstrInfo initialization routine.
1452 OS << "static inline void Init" << Target
Evan Cheng59ee62d2011-07-11 03:57:24 +00001453 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1454 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1455 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001456 if (NumFeatures)
1457 OS << Target << "FeatureKV, ";
1458 else
1459 OS << "0, ";
1460 if (NumProcs)
1461 OS << Target << "SubTypeKV, ";
1462 else
1463 OS << "0, ";
Andrew Trick544c8802012-09-17 22:18:50 +00001464 OS << '\n'; OS.indent(22);
Andrew Tricke127dfd2012-09-18 03:18:56 +00001465 OS << Target << "ProcSchedKV, "
1466 << Target << "WriteProcResTable, "
1467 << Target << "WriteLatencyTable, "
1468 << Target << "ReadAdvanceTable, ";
Andrew Trick1ab961f2013-03-16 18:58:55 +00001469 if (SchedModels.hasItineraries()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001470 OS << '\n'; OS.indent(22);
1471 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001472 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001473 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001474 } else
Andrew Tricke127dfd2012-09-18 03:18:56 +00001475 OS << "0, 0, 0, ";
Evan Cheng94214702011-07-01 20:45:01 +00001476 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
1477
1478 OS << "} // End llvm namespace \n";
1479
1480 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1481
1482 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1483 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1484
1485 OS << "#include \"llvm/Support/Debug.h\"\n";
1486 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
1487 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1488
1489 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1490
Evan Cheng5b1b44892011-07-01 21:01:15 +00001491 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
Evan Cheng94214702011-07-01 20:45:01 +00001492 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1493 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1494
1495 std::string ClassName = Target + "GenSubtargetInfo";
1496 OS << "namespace llvm {\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001497 OS << "class DFAPacketizer;\n";
Evan Cheng5b1b44892011-07-01 21:01:15 +00001498 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001499 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1500 << "StringRef FS);\n"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001501 << "public:\n"
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001502 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
1503 << " const TargetSchedModel *SchedModel) const;\n"
Sebastian Pop464f3a32011-12-06 17:34:16 +00001504 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001505 << " const;\n"
Evan Cheng94214702011-07-01 20:45:01 +00001506 << "};\n";
1507 OS << "} // End llvm namespace \n";
1508
1509 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1510
1511 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1512 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1513
Andrew Trickee290ba2012-09-18 03:32:57 +00001514 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
Evan Cheng94214702011-07-01 20:45:01 +00001515 OS << "namespace llvm {\n";
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001516 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1517 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001518 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1519 OS << "extern const llvm::MCWriteProcResEntry "
1520 << Target << "WriteProcResTable[];\n";
1521 OS << "extern const llvm::MCWriteLatencyEntry "
1522 << Target << "WriteLatencyTable[];\n";
1523 OS << "extern const llvm::MCReadAdvanceEntry "
1524 << Target << "ReadAdvanceTable[];\n";
1525
Andrew Trick1ab961f2013-03-16 18:58:55 +00001526 if (SchedModels.hasItineraries()) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001527 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1528 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
Andrew Tricka11a6282012-07-07 03:59:48 +00001529 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
Evan Chengc60f9b72011-07-14 20:59:42 +00001530 }
1531
Evan Cheng0ddff1b2011-07-07 07:07:08 +00001532 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1533 << "StringRef FS)\n"
Evan Cheng5b1b44892011-07-01 21:01:15 +00001534 << " : TargetSubtargetInfo() {\n"
Evan Cheng59ee62d2011-07-11 03:57:24 +00001535 << " InitMCSubtargetInfo(TT, CPU, FS, ";
Evan Cheng94214702011-07-01 20:45:01 +00001536 if (NumFeatures)
1537 OS << Target << "FeatureKV, ";
1538 else
1539 OS << "0, ";
1540 if (NumProcs)
1541 OS << Target << "SubTypeKV, ";
1542 else
1543 OS << "0, ";
Andrew Tricke127dfd2012-09-18 03:18:56 +00001544 OS << '\n'; OS.indent(22);
1545 OS << Target << "ProcSchedKV, "
1546 << Target << "WriteProcResTable, "
1547 << Target << "WriteLatencyTable, "
1548 << Target << "ReadAdvanceTable, ";
1549 OS << '\n'; OS.indent(22);
Andrew Trick1ab961f2013-03-16 18:58:55 +00001550 if (SchedModels.hasItineraries()) {
Andrew Tricke127dfd2012-09-18 03:18:56 +00001551 OS << Target << "Stages, "
Evan Cheng94214702011-07-01 20:45:01 +00001552 << Target << "OperandCycles, "
Andrew Tricka11a6282012-07-07 03:59:48 +00001553 << Target << "ForwardingPaths, ";
Evan Cheng94214702011-07-01 20:45:01 +00001554 } else
Andrew Tricke127dfd2012-09-18 03:18:56 +00001555 OS << "0, 0, 0, ";
Evan Cheng94214702011-07-01 20:45:01 +00001556 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
Andrew Trick544c8802012-09-17 22:18:50 +00001557
Andrew Trick4d2d1c42012-09-18 03:41:43 +00001558 EmitSchedModelHelpers(ClassName, OS);
1559
Evan Cheng94214702011-07-01 20:45:01 +00001560 OS << "} // End llvm namespace \n";
1561
1562 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
Jim Laskey4bb9cbb2005-10-21 19:00:04 +00001563}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001564
1565namespace llvm {
1566
1567void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
Andrew Trick2661b412012-07-07 04:00:00 +00001568 CodeGenTarget CGTarget(RK);
1569 SubtargetEmitter(RK, CGTarget).run(OS);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001570}
1571
1572} // End llvm namespace