Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 1 | //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 3d87811 | 2006-03-03 02:04:07 +0000 | [diff] [blame] | 10 | // This tablegen backend emits subtarget enumerations. |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Andrew Trick | fe05d98 | 2012-10-03 23:06:25 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "subtarget-emitter" |
| 15 | |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 16 | #include "CodeGenTarget.h" |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 17 | #include "CodeGenSchedule.h" |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 4ffd89f | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInstrItineraries.h" |
Chandler Carruth | 4ffd89f | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/Format.h" |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 23 | #include "llvm/TableGen/Error.h" |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 24 | #include "llvm/TableGen/Record.h" |
| 25 | #include "llvm/TableGen/TableGenBackend.h" |
Jeff Cohen | 9489c04 | 2005-10-28 01:43:09 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 27 | #include <map> |
| 28 | #include <string> |
| 29 | #include <vector> |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | class SubtargetEmitter { |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 34 | // 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 Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 41 | std::vector<std::string> WriterNames; |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 42 | 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 Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 49 | WriterNames.push_back("InvalidWrite"); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 50 | 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 Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 60 | |
| 61 | RecordKeeper &Records; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 62 | CodeGenSchedModels &SchedModels; |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 63 | std::string Target; |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 64 | |
| 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 Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 68 | 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 Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 76 | 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 Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 83 | char Separator); |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 84 | void EmitProcessorResources(const CodeGenProcModel &ProcModel, |
| 85 | raw_ostream &OS); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 86 | Record *FindWriteResources(const CodeGenSchedRW &SchedWrite, |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 87 | const CodeGenProcModel &ProcModel); |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 88 | Record *FindReadAdvance(const CodeGenSchedRW &SchedRead, |
| 89 | const CodeGenProcModel &ProcModel); |
Andrew Trick | 1754aca | 2013-03-14 21:21:50 +0000 | [diff] [blame] | 90 | void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles, |
| 91 | const CodeGenProcModel &ProcModel); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 92 | void GenSchedClassTables(const CodeGenProcModel &ProcModel, |
| 93 | SchedClassTables &SchedTables); |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 94 | void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 95 | void EmitProcessorModels(raw_ostream &OS); |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 96 | void EmitProcessorLookup(raw_ostream &OS); |
Andrew Trick | 4d2d1c4 | 2012-09-18 03:41:43 +0000 | [diff] [blame] | 97 | void EmitSchedModelHelpers(std::string ClassName, raw_ostream &OS); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 98 | void EmitSchedModel(raw_ostream &OS); |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 99 | void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures, |
| 100 | unsigned NumProcs); |
| 101 | |
| 102 | public: |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 103 | SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT): |
| 104 | Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {} |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 105 | |
| 106 | void run(raw_ostream &o); |
| 107 | |
| 108 | }; |
| 109 | } // End anonymous namespace |
| 110 | |
Jim Laskey | 7dc0204 | 2005-10-22 07:59:56 +0000 | [diff] [blame] | 111 | // |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 112 | // Enumeration - Emit the specified class as an enumeration. |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 113 | // |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 114 | void SubtargetEmitter::Enumeration(raw_ostream &OS, |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 115 | const char *ClassName, |
| 116 | bool isBits) { |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 117 | // Get all records of class and sort |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 118 | std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName); |
Duraid Madina | 42d24c7 | 2005-12-30 14:56:37 +0000 | [diff] [blame] | 119 | std::sort(DefList.begin(), DefList.end(), LessRecord()); |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 120 | |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 121 | unsigned N = DefList.size(); |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 122 | if (N == 0) |
| 123 | return; |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 124 | if (N > 64) { |
| 125 | errs() << "Too many (> 64) subtarget features!\n"; |
| 126 | exit(1); |
| 127 | } |
| 128 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 129 | OS << "namespace " << Target << " {\n"; |
| 130 | |
Jakob Stoklund Olesen | ac1ed44 | 2012-01-03 23:04:28 +0000 | [diff] [blame] | 131 | // 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 Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 138 | |
Jakob Stoklund Olesen | ac1ed44 | 2012-01-03 23:04:28 +0000 | [diff] [blame] | 139 | // 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 Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 145 | |
Jakob Stoklund Olesen | ac1ed44 | 2012-01-03 23:04:28 +0000 | [diff] [blame] | 146 | // For each record |
| 147 | for (unsigned i = 0; i < N;) { |
| 148 | // Next record |
| 149 | Record *Def = DefList[i]; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 150 | |
Jakob Stoklund Olesen | ac1ed44 | 2012-01-03 23:04:28 +0000 | [diff] [blame] | 151 | // Get and emit name |
| 152 | OS << " " << Def->getName(); |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 153 | |
Jakob Stoklund Olesen | ac1ed44 | 2012-01-03 23:04:28 +0000 | [diff] [blame] | 154 | // If bit flags then emit expression (1 << i) |
| 155 | if (isBits) OS << " = " << " 1ULL << " << i; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 156 | |
Jakob Stoklund Olesen | ac1ed44 | 2012-01-03 23:04:28 +0000 | [diff] [blame] | 157 | // 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 Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 165 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 166 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 167 | OS << "}\n"; |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 171 | // FeatureKeyValues - Emit data of all the subtarget features. Used by the |
| 172 | // command line. |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 173 | // |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 174 | unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) { |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 175 | // Gather and sort all the features |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 176 | std::vector<Record*> FeatureList = |
| 177 | Records.getAllDerivedDefinitions("SubtargetFeature"); |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 178 | |
| 179 | if (FeatureList.empty()) |
| 180 | return 0; |
| 181 | |
Jim Grosbach | 7c9a772 | 2008-09-11 17:05:32 +0000 | [diff] [blame] | 182 | std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName()); |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 183 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 184 | // Begin feature table |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 185 | OS << "// Sorted (by key) array of values for CPU features.\n" |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 186 | << "extern const llvm::SubtargetFeatureKV " << Target |
| 187 | << "FeatureKV[] = {\n"; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 188 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 189 | // For each feature |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 190 | unsigned NumFeatures = 0; |
Jim Laskey | dbe4006 | 2006-12-12 20:55:58 +0000 | [diff] [blame] | 191 | for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) { |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 192 | // Next feature |
| 193 | Record *Feature = FeatureList[i]; |
| 194 | |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 195 | const std::string &Name = Feature->getName(); |
| 196 | const std::string &CommandLineName = Feature->getValueAsString("Name"); |
| 197 | const std::string &Desc = Feature->getValueAsString("Desc"); |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 198 | |
Jim Laskey | dbe4006 | 2006-12-12 20:55:58 +0000 | [diff] [blame] | 199 | if (CommandLineName.empty()) continue; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 200 | |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 201 | // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in } |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 202 | OS << " { " |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 203 | << "\"" << CommandLineName << "\", " |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 204 | << "\"" << Desc << "\", " |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 205 | << Target << "::" << Name << ", "; |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 206 | |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 207 | const std::vector<Record*> &ImpliesList = |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 208 | Feature->getValueAsListOfDefs("Implies"); |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 209 | |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 210 | if (ImpliesList.empty()) { |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 211 | OS << "0ULL"; |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 212 | } else { |
| 213 | for (unsigned j = 0, M = ImpliesList.size(); j < M;) { |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 214 | OS << Target << "::" << ImpliesList[j]->getName(); |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 215 | if (++j < M) OS << " | "; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | OS << " }"; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 220 | ++NumFeatures; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 221 | |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 222 | // Depending on 'if more in the list' emit comma |
Jim Laskey | dbe4006 | 2006-12-12 20:55:58 +0000 | [diff] [blame] | 223 | if ((i + 1) < N) OS << ","; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 224 | |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 225 | OS << "\n"; |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 226 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 227 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 228 | // End feature table |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 229 | OS << "};\n"; |
| 230 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 231 | return NumFeatures; |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // |
| 235 | // CPUKeyValues - Emit data of all the subtarget processors. Used by command |
| 236 | // line. |
| 237 | // |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 238 | unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) { |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 239 | // Gather and sort processor information |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 240 | std::vector<Record*> ProcessorList = |
| 241 | Records.getAllDerivedDefinitions("Processor"); |
Duraid Madina | 42d24c7 | 2005-12-30 14:56:37 +0000 | [diff] [blame] | 242 | std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName()); |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 243 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 244 | // Begin processor table |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 245 | OS << "// Sorted (by key) array of values for CPU subtype.\n" |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 246 | << "extern const llvm::SubtargetFeatureKV " << Target |
| 247 | << "SubTypeKV[] = {\n"; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 248 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 249 | // For each processor |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 250 | for (unsigned i = 0, N = ProcessorList.size(); i < N;) { |
| 251 | // Next processor |
| 252 | Record *Processor = ProcessorList[i]; |
| 253 | |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 254 | const std::string &Name = Processor->getValueAsString("Name"); |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 255 | const std::vector<Record*> &FeatureList = |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 256 | Processor->getValueAsListOfDefs("Features"); |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 257 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 258 | // Emit as { "cpu", "description", f1 | f2 | ... fn }, |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 259 | OS << " { " |
| 260 | << "\"" << Name << "\", " |
| 261 | << "\"Select the " << Name << " processor\", "; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 262 | |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 263 | if (FeatureList.empty()) { |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 264 | OS << "0ULL"; |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 265 | } else { |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 266 | for (unsigned j = 0, M = FeatureList.size(); j < M;) { |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 267 | OS << Target << "::" << FeatureList[j]->getName(); |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 268 | if (++j < M) OS << " | "; |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 271 | |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 272 | // The "0" is for the "implies" section of this data structure. |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 273 | OS << ", 0ULL }"; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 274 | |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 275 | // Depending on 'if more in the list' emit comma |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 276 | if (++i < N) OS << ","; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 277 | |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 278 | OS << "\n"; |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 279 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 280 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 281 | // End processor table |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 282 | OS << "};\n"; |
| 283 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 284 | return ProcessorList.size(); |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame] | 285 | } |
Jim Laskey | 7dc0204 | 2005-10-22 07:59:56 +0000 | [diff] [blame] | 286 | |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 287 | // |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 288 | // FormItineraryStageString - Compose a string containing the stage |
| 289 | // data initialization for the specified itinerary. N is the number |
| 290 | // of stages. |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 291 | // |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 292 | void SubtargetEmitter::FormItineraryStageString(const std::string &Name, |
| 293 | Record *ItinData, |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 294 | std::string &ItinString, |
| 295 | unsigned &NStages) { |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 296 | // Get states list |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 297 | const std::vector<Record*> &StageList = |
| 298 | ItinData->getValueAsListOfDefs("Stages"); |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 299 | |
| 300 | // For each stage |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 301 | unsigned N = NStages = StageList.size(); |
Christopher Lamb | 8dadf6b | 2007-04-22 09:04:24 +0000 | [diff] [blame] | 302 | for (unsigned i = 0; i < N;) { |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 303 | // Next stage |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 304 | const Record *Stage = StageList[i]; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 305 | |
Anton Korobeynikov | 96085a3 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 306 | // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind } |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 307 | int Cycles = Stage->getValueAsInt("Cycles"); |
Jim Laskey | 7f39c14 | 2005-11-03 22:47:41 +0000 | [diff] [blame] | 308 | ItinString += " { " + itostr(Cycles) + ", "; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 309 | |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 310 | // Get unit list |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 311 | const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units"); |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 312 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 313 | // For each unit |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 314 | for (unsigned j = 0, M = UnitList.size(); j < M;) { |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 315 | // Add name and bitwise or |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 316 | ItinString += Name + "FU::" + UnitList[j]->getName(); |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 317 | if (++j < M) ItinString += " | "; |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 318 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 319 | |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 320 | int TimeInc = Stage->getValueAsInt("TimeInc"); |
| 321 | ItinString += ", " + itostr(TimeInc); |
| 322 | |
Anton Korobeynikov | 96085a3 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 323 | int Kind = Stage->getValueAsInt("Kind"); |
| 324 | ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind); |
| 325 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 326 | // Close off stage |
| 327 | ItinString += " }"; |
Christopher Lamb | 8dadf6b | 2007-04-22 09:04:24 +0000 | [diff] [blame] | 328 | if (++i < N) ItinString += ", "; |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 329 | } |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 333 | // 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 Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 336 | // |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 337 | void 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 Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 348 | |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 349 | ItinString += " " + itostr(OCycle); |
| 350 | if (++i < N) ItinString += ", "; |
| 351 | } |
| 352 | } |
| 353 | |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 354 | void 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 Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 361 | unsigned i = 0; |
| 362 | for (; i < N;) { |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 363 | ItinString += Name + "Bypass::" + BypassList[i]->getName(); |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 364 | if (++i < NOperandCycles) ItinString += ", "; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 365 | } |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 366 | for (; i < NOperandCycles;) { |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 367 | ItinString += " 0"; |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 368 | if (++i < NOperandCycles) ItinString += ", "; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 372 | // |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 373 | // EmitStageAndOperandCycleData - Generate unique itinerary stages and operand |
| 374 | // cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed |
| 375 | // by CodeGenSchedClass::Index. |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 376 | // |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 377 | void SubtargetEmitter:: |
| 378 | EmitStageAndOperandCycleData(raw_ostream &OS, |
| 379 | std::vector<std::vector<InstrItinerary> > |
| 380 | &ProcItinLists) { |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 381 | |
Andrew Trick | cb94192 | 2012-07-09 20:43:03 +0000 | [diff] [blame] | 382 | // Multiple processor models may share an itinerary record. Emit it once. |
| 383 | SmallPtrSet<Record*, 8> ItinsDefSet; |
| 384 | |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 385 | // Emit functional units for all the itineraries. |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 386 | for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), |
| 387 | PE = SchedModels.procModelEnd(); PI != PE; ++PI) { |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 388 | |
Andrew Trick | cb94192 | 2012-07-09 20:43:03 +0000 | [diff] [blame] | 389 | if (!ItinsDefSet.insert(PI->ItinsDef)) |
| 390 | continue; |
| 391 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 392 | std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU"); |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 393 | if (FUs.empty()) |
| 394 | continue; |
| 395 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 396 | const std::string &Name = PI->ItinsDef->getName(); |
| 397 | OS << "\n// Functional units for \"" << Name << "\"\n" |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 398 | << "namespace " << Name << "FU {\n"; |
| 399 | |
| 400 | for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j) |
Hal Finkel | b460a33 | 2012-06-22 20:27:13 +0000 | [diff] [blame] | 401 | OS << " const unsigned " << FUs[j]->getName() |
| 402 | << " = 1 << " << j << ";\n"; |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 403 | |
| 404 | OS << "}\n"; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 405 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 406 | std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP"); |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 407 | if (BPs.size()) { |
| 408 | OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name |
| 409 | << "\"\n" << "namespace " << Name << "Bypass {\n"; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 410 | |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 411 | OS << " const unsigned NoBypass = 0;\n"; |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 412 | for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j) |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 413 | OS << " const unsigned " << BPs[j]->getName() |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 414 | << " = 1 << " << j << ";\n"; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 415 | |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 416 | OS << "}\n"; |
| 417 | } |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 420 | // Begin stages table |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 421 | std::string StageTable = "\nextern const llvm::InstrStage " + Target + |
| 422 | "Stages[] = {\n"; |
Anton Korobeynikov | 96085a3 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 423 | StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n"; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 424 | |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 425 | // Begin operand cycle table |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 426 | std::string OperandCycleTable = "extern const unsigned " + Target + |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 427 | "OperandCycles[] = {\n"; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 428 | OperandCycleTable += " 0, // No itinerary\n"; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 429 | |
| 430 | // Begin pipeline bypass table |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 431 | std::string BypassTable = "extern const unsigned " + Target + |
Andrew Trick | a11a628 | 2012-07-07 03:59:48 +0000 | [diff] [blame] | 432 | "ForwardingPaths[] = {\n"; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 433 | BypassTable += " 0, // No itinerary\n"; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 434 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 435 | // 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 Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 438 | unsigned StageCount = 1, OperandCycleCount = 1; |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 439 | std::map<std::string, unsigned> ItinStageMap, ItinOperandMap; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 440 | for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), |
| 441 | PE = SchedModels.procModelEnd(); PI != PE; ++PI) { |
| 442 | const CodeGenProcModel &ProcModel = *PI; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 443 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 444 | // Add process itinerary to the list. |
| 445 | ProcItinLists.resize(ProcItinLists.size()+1); |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 446 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 447 | // If this processor defines no itineraries, then leave the itinerary list |
| 448 | // empty. |
| 449 | std::vector<InstrItinerary> &ItinList = ProcItinLists.back(); |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 450 | if (!ProcModel.hasItineraries()) |
Andrew Trick | d85934b | 2012-06-22 03:58:51 +0000 | [diff] [blame] | 451 | continue; |
Andrew Trick | d85934b | 2012-06-22 03:58:51 +0000 | [diff] [blame] | 452 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 453 | const std::string &Name = ProcModel.ItinsDef->getName(); |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 454 | |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 455 | ItinList.resize(SchedModels.numInstrSchedClasses()); |
| 456 | assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins"); |
| 457 | |
| 458 | for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size(); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 459 | SchedClassIdx < SchedClassEnd; ++SchedClassIdx) { |
| 460 | |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 461 | // Next itinerary data |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 462 | Record *ItinData = ProcModel.ItinDefList[SchedClassIdx]; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 463 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 464 | // Get string and stage count |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 465 | std::string ItinStageString; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 466 | unsigned NStages = 0; |
| 467 | if (ItinData) |
| 468 | FormItineraryStageString(Name, ItinData, ItinStageString, NStages); |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 469 | |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 470 | // Get string and operand cycle count |
| 471 | std::string ItinOperandCycleString; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 472 | unsigned NOperandCycles = 0; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 473 | std::string ItinBypassString; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 474 | if (ItinData) { |
| 475 | FormItineraryOperandCycleString(ItinData, ItinOperandCycleString, |
| 476 | NOperandCycles); |
| 477 | |
| 478 | FormItineraryBypassString(Name, ItinData, ItinBypassString, |
| 479 | NOperandCycles); |
| 480 | } |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 481 | |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 482 | // 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 Trick | 2348232 | 2011-04-01 02:22:47 +0000 | [diff] [blame] | 487 | // 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 Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 492 | // Record Itin class number. |
| 493 | ItinStageMap[ItinStageString] = FindStage = StageCount; |
| 494 | StageCount += NStages; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 497 | |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 498 | // Check to see if operand cycle already exists and create if it doesn't |
| 499 | unsigned FindOperandCycle = 0; |
| 500 | if (NOperandCycles > 0) { |
Evan Cheng | 3881cb7 | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 501 | std::string ItinOperandString = ItinOperandCycleString+ItinBypassString; |
| 502 | FindOperandCycle = ItinOperandMap[ItinOperandString]; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 503 | if (FindOperandCycle == 0) { |
| 504 | // Emit as cycle, // index |
Andrew Trick | 2348232 | 2011-04-01 02:22:47 +0000 | [diff] [blame] | 505 | OperandCycleTable += ItinOperandCycleString + ", // "; |
| 506 | std::string OperandIdxComment = itostr(OperandCycleCount); |
| 507 | if (NOperandCycles > 1) |
| 508 | OperandIdxComment += "-" |
| 509 | + itostr(OperandCycleCount + NOperandCycles - 1); |
| 510 | OperandCycleTable += OperandIdxComment + "\n"; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 511 | // Record Itin class number. |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 512 | ItinOperandMap[ItinOperandCycleString] = |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 513 | FindOperandCycle = OperandCycleCount; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 514 | // Emit as bypass, // index |
Andrew Trick | 2348232 | 2011-04-01 02:22:47 +0000 | [diff] [blame] | 515 | BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n"; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 516 | OperandCycleCount += NOperandCycles; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 517 | } |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 518 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 519 | |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 520 | // Set up itinerary as location and location + stage count |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 521 | int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0; |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 522 | InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages, |
| 523 | FindOperandCycle, |
| 524 | FindOperandCycle + NOperandCycles}; |
| 525 | |
Jim Laskey | 908ae27 | 2005-10-28 15:20:43 +0000 | [diff] [blame] | 526 | // Inject - empty slots will be 0, 0 |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 527 | ItinList[SchedClassIdx] = Intinerary; |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 528 | } |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 529 | } |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 530 | |
Jim Laskey | 7f39c14 | 2005-11-03 22:47:41 +0000 | [diff] [blame] | 531 | // Closing stage |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 532 | StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n"; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 533 | StageTable += "};\n"; |
| 534 | |
| 535 | // Closing operand cycles |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 536 | OperandCycleTable += " 0 // End operand cycles\n"; |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 537 | OperandCycleTable += "};\n"; |
| 538 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 539 | BypassTable += " 0 // End bypass tables\n"; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 540 | BypassTable += "};\n"; |
| 541 | |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 542 | // Emit tables. |
| 543 | OS << StageTable; |
| 544 | OS << OperandCycleTable; |
Evan Cheng | 63d66ee | 2010-09-28 23:50:49 +0000 | [diff] [blame] | 545 | OS << BypassTable; |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 548 | // |
| 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 | // |
| 554 | void SubtargetEmitter:: |
| 555 | EmitItineraries(raw_ostream &OS, |
| 556 | std::vector<std::vector<InstrItinerary> > &ProcItinLists) { |
| 557 | |
Andrew Trick | cb94192 | 2012-07-09 20:43:03 +0000 | [diff] [blame] | 558 | // Multiple processor models may share an itinerary record. Emit it once. |
| 559 | SmallPtrSet<Record*, 8> ItinsDefSet; |
| 560 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 561 | // 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 Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 565 | PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) { |
Andrew Trick | cb94192 | 2012-07-09 20:43:03 +0000 | [diff] [blame] | 566 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 567 | Record *ItinsDef = PI->ItinsDef; |
Andrew Trick | cb94192 | 2012-07-09 20:43:03 +0000 | [diff] [blame] | 568 | if (!ItinsDefSet.insert(ItinsDef)) |
| 569 | continue; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 570 | |
| 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 Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 576 | std::vector<InstrItinerary> &ItinList = *ProcItinListsIter; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 577 | |
| 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 Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 608 | // Emit either the value defined in the TableGen Record, or the default |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 609 | // value defined in the C++ header. The Record is null if the processor does not |
| 610 | // define a model. |
| 611 | void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R, |
Andrew Trick | fc99299 | 2012-06-05 03:44:40 +0000 | [diff] [blame] | 612 | const char *Name, char Separator) { |
| 613 | OS << " "; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 614 | int V = R ? R->getValueAsInt(Name) : -1; |
Andrew Trick | fc99299 | 2012-06-05 03:44:40 +0000 | [diff] [blame] | 615 | if (V >= 0) |
| 616 | OS << V << Separator << " // " << Name; |
| 617 | else |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 618 | OS << "MCSchedModel::Default" << Name << Separator; |
Andrew Trick | fc99299 | 2012-06-05 03:44:40 +0000 | [diff] [blame] | 619 | OS << '\n'; |
| 620 | } |
| 621 | |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 622 | void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel, |
| 623 | raw_ostream &OS) { |
| 624 | char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ','; |
| 625 | |
Andrew Trick | 6312cb0 | 2012-10-10 05:43:04 +0000 | [diff] [blame] | 626 | OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n"; |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 627 | OS << "static const llvm::MCProcResourceDesc " |
| 628 | << ProcModel.ModelName << "ProcResources" << "[] = {\n" |
Andrew Trick | 6312cb0 | 2012-10-10 05:43:04 +0000 | [diff] [blame] | 629 | << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n"; |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 630 | |
| 631 | for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) { |
| 632 | Record *PRDef = ProcModel.ProcResourceDefs[i]; |
| 633 | |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 634 | Record *SuperDef = 0; |
Andrew Trick | 1754aca | 2013-03-14 21:21:50 +0000 | [diff] [blame] | 635 | 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 Trick | 157c6c4 | 2013-03-14 22:47:01 +0000 | [diff] [blame] | 657 | NumUnits = PRDef->getValueAsInt("NumUnits"); |
| 658 | IsBuffered = PRDef->getValueAsBit("Buffered"); |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 659 | } |
| 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 Trick | 1754aca | 2013-03-14 21:21:50 +0000 | [diff] [blame] | 666 | OS << NumUnits << ", " << SuperIdx << ", " |
| 667 | << IsBuffered << "}" << Sep << " // #" << i+1; |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 668 | if (SuperDef) |
| 669 | OS << ", Super=" << SuperDef->getName(); |
| 670 | OS << "\n"; |
| 671 | } |
| 672 | OS << "};\n"; |
| 673 | } |
| 674 | |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 675 | // Find the WriteRes Record that defines processor resources for this |
| 676 | // SchedWrite. |
| 677 | Record *SubtargetEmitter::FindWriteResources( |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 678 | const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) { |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 679 | |
| 680 | // Check if the SchedWrite is already subtarget-specific and directly |
| 681 | // specifies a set of processor resources. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 682 | if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes")) |
| 683 | return SchedWrite.TheDef; |
| 684 | |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 685 | 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 Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 690 | if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) { |
| 691 | Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel"); |
| 692 | if (&SchedModels.getProcModel(ModelDef) != &ProcModel) |
| 693 | continue; |
| 694 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 695 | if (AliasDef) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 696 | PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases " |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 697 | "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 Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 703 | |
| 704 | // Check this processor's list of write resources. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 705 | Record *ResDef = 0; |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 706 | for (RecIter WRI = ProcModel.WriteResDefs.begin(), |
| 707 | WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) { |
| 708 | if (!(*WRI)->isSubClassOf("WriteRes")) |
| 709 | continue; |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 710 | if (AliasDef == (*WRI)->getValueAsDef("WriteType") |
| 711 | || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) { |
| 712 | if (ResDef) { |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 713 | PrintFatalError((*WRI)->getLoc(), "Resources are defined for both " |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 714 | "SchedWrite and its alias on processor " + |
| 715 | ProcModel.ModelName); |
| 716 | } |
| 717 | ResDef = *WRI; |
| 718 | } |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 719 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 720 | // TODO: If ProcModel has a base model (previous generation processor), |
| 721 | // then call FindWriteResources recursively with that model here. |
| 722 | if (!ResDef) { |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 723 | PrintFatalError(ProcModel.ModelDef->getLoc(), |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 724 | std::string("Processor does not define resources for ") |
| 725 | + SchedWrite.TheDef->getName()); |
| 726 | } |
| 727 | return ResDef; |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /// Find the ReadAdvance record for the given SchedRead on this processor or |
| 731 | /// return NULL. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 732 | Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead, |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 733 | const CodeGenProcModel &ProcModel) { |
| 734 | // Check for SchedReads that directly specify a ReadAdvance. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 735 | 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 Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 744 | if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) { |
| 745 | Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel"); |
| 746 | if (&SchedModels.getProcModel(ModelDef) != &ProcModel) |
| 747 | continue; |
| 748 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 749 | if (AliasDef) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 750 | PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases " |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 751 | "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 Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 757 | |
| 758 | // Check this processor's ReadAdvanceList. |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 759 | Record *ResDef = 0; |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 760 | for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(), |
| 761 | RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) { |
| 762 | if (!(*RAI)->isSubClassOf("ReadAdvance")) |
| 763 | continue; |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 764 | if (AliasDef == (*RAI)->getValueAsDef("ReadType") |
| 765 | || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) { |
| 766 | if (ResDef) { |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 767 | PrintFatalError((*RAI)->getLoc(), "Resources are defined for both " |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 768 | "SchedRead and its alias on processor " + |
| 769 | ProcModel.ModelName); |
| 770 | } |
| 771 | ResDef = *RAI; |
| 772 | } |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 773 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 774 | // 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 Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 777 | PrintFatalError(ProcModel.ModelDef->getLoc(), |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 778 | std::string("Processor does not define resources for ") |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 779 | + SchedRead.TheDef->getName()); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 780 | } |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 781 | return ResDef; |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Andrew Trick | 1754aca | 2013-03-14 21:21:50 +0000 | [diff] [blame] | 784 | // 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. |
| 791 | void 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"); |
| 800 | std::sort(SubResources.begin(), SubResources.end(), LessRecord()); |
| 801 | } |
| 802 | else { |
| 803 | SubResources.push_back(PRVec[i]); |
| 804 | } |
| 805 | for (RecIter PRI = ProcModel.ProcResourceDefs.begin(), |
| 806 | PRE = ProcModel.ProcResourceDefs.end(); |
| 807 | PRI != PRE; ++PRI) { |
| 808 | if (*PRI == PRVec[i] || !(*PRI)->isSubClassOf("ProcResGroup")) |
| 809 | continue; |
| 810 | RecVec SuperResources = (*PRI)->getValueAsListOfDefs("Resources"); |
| 811 | std::sort(SuperResources.begin(), SuperResources.end(), LessRecord()); |
| 812 | RecIter SubI = SubResources.begin(), SubE = SubResources.end(); |
| 813 | RecIter SuperI = SuperResources.begin(), SuperE = SuperResources.end(); |
| 814 | for ( ; SubI != SubE && SuperI != SuperE; ++SuperI) { |
| 815 | if (*SubI < *SuperI) |
| 816 | break; |
| 817 | else if (*SuperI < *SubI) |
| 818 | continue; |
| 819 | ++SubI; |
| 820 | } |
| 821 | if (SubI == SubE) { |
| 822 | PRVec.push_back(*PRI); |
| 823 | Cycles.push_back(Cycles[i]); |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 829 | // Generate the SchedClass table for this processor and update global |
| 830 | // tables. Must be called for each processor in order. |
| 831 | void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel, |
| 832 | SchedClassTables &SchedTables) { |
| 833 | SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1); |
| 834 | if (!ProcModel.hasInstrSchedModel()) |
| 835 | return; |
| 836 | |
| 837 | std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back(); |
| 838 | for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(), |
| 839 | SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) { |
Andrew Trick | fe05d98 | 2012-10-03 23:06:25 +0000 | [diff] [blame] | 840 | DEBUG(SCI->dump(&SchedModels)); |
| 841 | |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 842 | SCTab.resize(SCTab.size() + 1); |
| 843 | MCSchedClassDesc &SCDesc = SCTab.back(); |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 844 | // SCDesc.Name is guarded by NDEBUG |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 845 | SCDesc.NumMicroOps = 0; |
| 846 | SCDesc.BeginGroup = false; |
| 847 | SCDesc.EndGroup = false; |
| 848 | SCDesc.WriteProcResIdx = 0; |
| 849 | SCDesc.WriteLatencyIdx = 0; |
| 850 | SCDesc.ReadAdvanceIdx = 0; |
| 851 | |
| 852 | // A Variant SchedClass has no resources of its own. |
Andrew Trick | 82e7c4f | 2013-03-26 21:36:39 +0000 | [diff] [blame^] | 853 | bool HasVariants = false; |
| 854 | for (std::vector<CodeGenSchedTransition>::const_iterator |
| 855 | TI = SCI->Transitions.begin(), TE = SCI->Transitions.end(); |
| 856 | TI != TE; ++TI) { |
| 857 | if (TI->ProcIndices[0] == 0) { |
| 858 | HasVariants = true; |
| 859 | break; |
| 860 | } |
| 861 | IdxIter PIPos = std::find(TI->ProcIndices.begin(), |
| 862 | TI->ProcIndices.end(), ProcModel.Index); |
| 863 | if (PIPos != TI->ProcIndices.end()) { |
| 864 | HasVariants = true; |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | if (HasVariants) { |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 869 | SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps; |
| 870 | continue; |
| 871 | } |
| 872 | |
| 873 | // Determine if the SchedClass is actually reachable on this processor. If |
| 874 | // not don't try to locate the processor resources, it will fail. |
| 875 | // If ProcIndices contains 0, this class applies to all processors. |
| 876 | assert(!SCI->ProcIndices.empty() && "expect at least one procidx"); |
| 877 | if (SCI->ProcIndices[0] != 0) { |
| 878 | IdxIter PIPos = std::find(SCI->ProcIndices.begin(), |
| 879 | SCI->ProcIndices.end(), ProcModel.Index); |
| 880 | if (PIPos == SCI->ProcIndices.end()) |
| 881 | continue; |
| 882 | } |
| 883 | IdxVec Writes = SCI->Writes; |
| 884 | IdxVec Reads = SCI->Reads; |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 885 | if (!SCI->InstRWs.empty()) { |
| 886 | // This class has a default ReadWrite list which can be overriden by |
Andrew Trick | fe05d98 | 2012-10-03 23:06:25 +0000 | [diff] [blame] | 887 | // InstRW definitions. |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 888 | Record *RWDef = 0; |
| 889 | for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end(); |
| 890 | RWI != RWE; ++RWI) { |
| 891 | Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel"); |
| 892 | if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) { |
| 893 | RWDef = *RWI; |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | if (RWDef) { |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 898 | Writes.clear(); |
| 899 | Reads.clear(); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 900 | SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"), |
| 901 | Writes, Reads); |
| 902 | } |
| 903 | } |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 904 | if (Writes.empty()) { |
| 905 | // Check this processor's itinerary class resources. |
| 906 | for (RecIter II = ProcModel.ItinRWDefs.begin(), |
| 907 | IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) { |
| 908 | RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses"); |
| 909 | if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef) |
| 910 | != Matched.end()) { |
| 911 | SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), |
| 912 | Writes, Reads); |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | if (Writes.empty()) { |
| 917 | DEBUG(dbgs() << ProcModel.ModelName |
| 918 | << " does not have resources for class " << SCI->Name << '\n'); |
| 919 | } |
| 920 | } |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 921 | // Sum resources across all operand writes. |
| 922 | std::vector<MCWriteProcResEntry> WriteProcResources; |
| 923 | std::vector<MCWriteLatencyEntry> WriteLatencies; |
Andrew Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 924 | std::vector<std::string> WriterNames; |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 925 | std::vector<MCReadAdvanceEntry> ReadAdvanceEntries; |
| 926 | for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) { |
| 927 | IdxVec WriteSeq; |
Andrew Trick | 2062b12 | 2012-10-03 23:06:28 +0000 | [diff] [blame] | 928 | SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false, |
| 929 | ProcModel); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 930 | |
| 931 | // For each operand, create a latency entry. |
| 932 | MCWriteLatencyEntry WLEntry; |
| 933 | WLEntry.Cycles = 0; |
Andrew Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 934 | unsigned WriteID = WriteSeq.back(); |
| 935 | WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name); |
| 936 | // If this Write is not referenced by a ReadAdvance, don't distinguish it |
| 937 | // from other WriteLatency entries. |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 938 | if (!SchedModels.hasReadOfWrite( |
| 939 | SchedModels.getSchedWrite(WriteID).TheDef)) { |
Andrew Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 940 | WriteID = 0; |
| 941 | } |
| 942 | WLEntry.WriteResourceID = WriteID; |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 943 | |
| 944 | for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end(); |
| 945 | WSI != WSE; ++WSI) { |
| 946 | |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 947 | Record *WriteRes = |
| 948 | FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 949 | |
| 950 | // Mark the parent class as invalid for unsupported write types. |
| 951 | if (WriteRes->getValueAsBit("Unsupported")) { |
| 952 | SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps; |
| 953 | break; |
| 954 | } |
| 955 | WLEntry.Cycles += WriteRes->getValueAsInt("Latency"); |
| 956 | SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps"); |
| 957 | SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup"); |
| 958 | SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup"); |
| 959 | |
| 960 | // Create an entry for each ProcResource listed in WriteRes. |
| 961 | RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources"); |
| 962 | std::vector<int64_t> Cycles = |
| 963 | WriteRes->getValueAsListOfInts("ResourceCycles"); |
Andrew Trick | 1754aca | 2013-03-14 21:21:50 +0000 | [diff] [blame] | 964 | |
| 965 | ExpandProcResources(PRVec, Cycles, ProcModel); |
| 966 | |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 967 | for (unsigned PRIdx = 0, PREnd = PRVec.size(); |
| 968 | PRIdx != PREnd; ++PRIdx) { |
| 969 | MCWriteProcResEntry WPREntry; |
| 970 | WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]); |
| 971 | assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx"); |
Andrew Trick | 1754aca | 2013-03-14 21:21:50 +0000 | [diff] [blame] | 972 | WPREntry.Cycles = Cycles[PRIdx]; |
Andrew Trick | c812110 | 2013-03-01 23:31:26 +0000 | [diff] [blame] | 973 | // If this resource is already used in this sequence, add the current |
| 974 | // entry's cycles so that the same resource appears to be used |
| 975 | // serially, rather than multiple parallel uses. This is important for |
| 976 | // in-order machine where the resource consumption is a hazard. |
| 977 | unsigned WPRIdx = 0, WPREnd = WriteProcResources.size(); |
| 978 | for( ; WPRIdx != WPREnd; ++WPRIdx) { |
| 979 | if (WriteProcResources[WPRIdx].ProcResourceIdx |
| 980 | == WPREntry.ProcResourceIdx) { |
| 981 | WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles; |
| 982 | break; |
| 983 | } |
| 984 | } |
| 985 | if (WPRIdx == WPREnd) |
| 986 | WriteProcResources.push_back(WPREntry); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | WriteLatencies.push_back(WLEntry); |
| 990 | } |
| 991 | // Create an entry for each operand Read in this SchedClass. |
| 992 | // Entries must be sorted first by UseIdx then by WriteResourceID. |
| 993 | for (unsigned UseIdx = 0, EndIdx = Reads.size(); |
| 994 | UseIdx != EndIdx; ++UseIdx) { |
Andrew Trick | 9264988 | 2012-09-22 02:24:21 +0000 | [diff] [blame] | 995 | Record *ReadAdvance = |
| 996 | FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 997 | if (!ReadAdvance) |
| 998 | continue; |
| 999 | |
| 1000 | // Mark the parent class as invalid for unsupported write types. |
| 1001 | if (ReadAdvance->getValueAsBit("Unsupported")) { |
| 1002 | SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps; |
| 1003 | break; |
| 1004 | } |
| 1005 | RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites"); |
| 1006 | IdxVec WriteIDs; |
| 1007 | if (ValidWrites.empty()) |
| 1008 | WriteIDs.push_back(0); |
| 1009 | else { |
| 1010 | for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end(); |
| 1011 | VWI != VWE; ++VWI) { |
| 1012 | WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false)); |
| 1013 | } |
| 1014 | } |
| 1015 | std::sort(WriteIDs.begin(), WriteIDs.end()); |
| 1016 | for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) { |
| 1017 | MCReadAdvanceEntry RAEntry; |
| 1018 | RAEntry.UseIdx = UseIdx; |
| 1019 | RAEntry.WriteResourceID = *WI; |
| 1020 | RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles"); |
| 1021 | ReadAdvanceEntries.push_back(RAEntry); |
| 1022 | } |
| 1023 | } |
| 1024 | if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) { |
| 1025 | WriteProcResources.clear(); |
| 1026 | WriteLatencies.clear(); |
| 1027 | ReadAdvanceEntries.clear(); |
| 1028 | } |
| 1029 | // Add the information for this SchedClass to the global tables using basic |
| 1030 | // compression. |
| 1031 | // |
| 1032 | // WritePrecRes entries are sorted by ProcResIdx. |
| 1033 | std::sort(WriteProcResources.begin(), WriteProcResources.end(), |
| 1034 | LessWriteProcResources()); |
| 1035 | |
| 1036 | SCDesc.NumWriteProcResEntries = WriteProcResources.size(); |
| 1037 | std::vector<MCWriteProcResEntry>::iterator WPRPos = |
| 1038 | std::search(SchedTables.WriteProcResources.begin(), |
| 1039 | SchedTables.WriteProcResources.end(), |
| 1040 | WriteProcResources.begin(), WriteProcResources.end()); |
| 1041 | if (WPRPos != SchedTables.WriteProcResources.end()) |
| 1042 | SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin(); |
| 1043 | else { |
| 1044 | SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size(); |
| 1045 | SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(), |
| 1046 | WriteProcResources.end()); |
| 1047 | } |
| 1048 | // Latency entries must remain in operand order. |
| 1049 | SCDesc.NumWriteLatencyEntries = WriteLatencies.size(); |
| 1050 | std::vector<MCWriteLatencyEntry>::iterator WLPos = |
| 1051 | std::search(SchedTables.WriteLatencies.begin(), |
| 1052 | SchedTables.WriteLatencies.end(), |
| 1053 | WriteLatencies.begin(), WriteLatencies.end()); |
Andrew Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 1054 | if (WLPos != SchedTables.WriteLatencies.end()) { |
| 1055 | unsigned idx = WLPos - SchedTables.WriteLatencies.begin(); |
| 1056 | SCDesc.WriteLatencyIdx = idx; |
| 1057 | for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i) |
| 1058 | if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) == |
| 1059 | std::string::npos) { |
| 1060 | SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i]; |
| 1061 | } |
| 1062 | } |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 1063 | else { |
| 1064 | SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size(); |
Andrew Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 1065 | SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(), |
| 1066 | WriteLatencies.begin(), |
| 1067 | WriteLatencies.end()); |
| 1068 | SchedTables.WriterNames.insert(SchedTables.WriterNames.end(), |
| 1069 | WriterNames.begin(), WriterNames.end()); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 1070 | } |
| 1071 | // ReadAdvanceEntries must remain in operand order. |
| 1072 | SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size(); |
| 1073 | std::vector<MCReadAdvanceEntry>::iterator RAPos = |
| 1074 | std::search(SchedTables.ReadAdvanceEntries.begin(), |
| 1075 | SchedTables.ReadAdvanceEntries.end(), |
| 1076 | ReadAdvanceEntries.begin(), ReadAdvanceEntries.end()); |
| 1077 | if (RAPos != SchedTables.ReadAdvanceEntries.end()) |
| 1078 | SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin(); |
| 1079 | else { |
| 1080 | SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size(); |
| 1081 | SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(), |
| 1082 | ReadAdvanceEntries.end()); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1087 | // Emit SchedClass tables for all processors and associated global tables. |
| 1088 | void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables, |
| 1089 | raw_ostream &OS) { |
| 1090 | // Emit global WriteProcResTable. |
| 1091 | OS << "\n// {ProcResourceIdx, Cycles}\n" |
| 1092 | << "extern const llvm::MCWriteProcResEntry " |
| 1093 | << Target << "WriteProcResTable[] = {\n" |
| 1094 | << " { 0, 0}, // Invalid\n"; |
| 1095 | for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size(); |
| 1096 | WPRIdx != WPREnd; ++WPRIdx) { |
| 1097 | MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx]; |
| 1098 | OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", " |
| 1099 | << format("%2d", WPREntry.Cycles) << "}"; |
| 1100 | if (WPRIdx + 1 < WPREnd) |
| 1101 | OS << ','; |
| 1102 | OS << " // #" << WPRIdx << '\n'; |
| 1103 | } |
| 1104 | OS << "}; // " << Target << "WriteProcResTable\n"; |
| 1105 | |
| 1106 | // Emit global WriteLatencyTable. |
| 1107 | OS << "\n// {Cycles, WriteResourceID}\n" |
| 1108 | << "extern const llvm::MCWriteLatencyEntry " |
| 1109 | << Target << "WriteLatencyTable[] = {\n" |
| 1110 | << " { 0, 0}, // Invalid\n"; |
| 1111 | for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size(); |
| 1112 | WLIdx != WLEnd; ++WLIdx) { |
| 1113 | MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx]; |
| 1114 | OS << " {" << format("%2d", WLEntry.Cycles) << ", " |
| 1115 | << format("%2d", WLEntry.WriteResourceID) << "}"; |
| 1116 | if (WLIdx + 1 < WLEnd) |
| 1117 | OS << ','; |
Andrew Trick | 3b8fb64 | 2012-09-19 04:43:19 +0000 | [diff] [blame] | 1118 | OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n'; |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1119 | } |
| 1120 | OS << "}; // " << Target << "WriteLatencyTable\n"; |
| 1121 | |
| 1122 | // Emit global ReadAdvanceTable. |
| 1123 | OS << "\n// {UseIdx, WriteResourceID, Cycles}\n" |
| 1124 | << "extern const llvm::MCReadAdvanceEntry " |
| 1125 | << Target << "ReadAdvanceTable[] = {\n" |
| 1126 | << " {0, 0, 0}, // Invalid\n"; |
| 1127 | for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size(); |
| 1128 | RAIdx != RAEnd; ++RAIdx) { |
| 1129 | MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx]; |
| 1130 | OS << " {" << RAEntry.UseIdx << ", " |
| 1131 | << format("%2d", RAEntry.WriteResourceID) << ", " |
| 1132 | << format("%2d", RAEntry.Cycles) << "}"; |
| 1133 | if (RAIdx + 1 < RAEnd) |
| 1134 | OS << ','; |
| 1135 | OS << " // #" << RAIdx << '\n'; |
| 1136 | } |
| 1137 | OS << "}; // " << Target << "ReadAdvanceTable\n"; |
| 1138 | |
| 1139 | // Emit a SchedClass table for each processor. |
| 1140 | for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), |
| 1141 | PE = SchedModels.procModelEnd(); PI != PE; ++PI) { |
| 1142 | if (!PI->hasInstrSchedModel()) |
| 1143 | continue; |
| 1144 | |
| 1145 | std::vector<MCSchedClassDesc> &SCTab = |
Rafael Espindola | 322ff88 | 2012-11-02 20:57:36 +0000 | [diff] [blame] | 1146 | SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())]; |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1147 | |
| 1148 | OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup," |
| 1149 | << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n"; |
| 1150 | OS << "static const llvm::MCSchedClassDesc " |
| 1151 | << PI->ModelName << "SchedClasses[] = {\n"; |
| 1152 | |
| 1153 | // The first class is always invalid. We no way to distinguish it except by |
| 1154 | // name and position. |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1155 | assert(SchedModels.getSchedClass(0).Name == "NoInstrModel" |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1156 | && "invalid class not first"); |
| 1157 | OS << " {DBGFIELD(\"InvalidSchedClass\") " |
| 1158 | << MCSchedClassDesc::InvalidNumMicroOps |
| 1159 | << ", 0, 0, 0, 0, 0, 0, 0, 0},\n"; |
| 1160 | |
| 1161 | for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) { |
| 1162 | MCSchedClassDesc &MCDesc = SCTab[SCIdx]; |
| 1163 | const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx); |
| 1164 | OS << " {DBGFIELD(\"" << SchedClass.Name << "\") "; |
| 1165 | if (SchedClass.Name.size() < 18) |
| 1166 | OS.indent(18 - SchedClass.Name.size()); |
| 1167 | OS << MCDesc.NumMicroOps |
| 1168 | << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup |
| 1169 | << ", " << format("%2d", MCDesc.WriteProcResIdx) |
| 1170 | << ", " << MCDesc.NumWriteProcResEntries |
| 1171 | << ", " << format("%2d", MCDesc.WriteLatencyIdx) |
| 1172 | << ", " << MCDesc.NumWriteLatencyEntries |
| 1173 | << ", " << format("%2d", MCDesc.ReadAdvanceIdx) |
| 1174 | << ", " << MCDesc.NumReadAdvanceEntries << "}"; |
| 1175 | if (SCIdx + 1 < SCEnd) |
| 1176 | OS << ','; |
| 1177 | OS << " // #" << SCIdx << '\n'; |
| 1178 | } |
| 1179 | OS << "}; // " << PI->ModelName << "SchedClasses\n"; |
| 1180 | } |
| 1181 | } |
| 1182 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1183 | void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) { |
| 1184 | // For each processor model. |
| 1185 | for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), |
| 1186 | PE = SchedModels.procModelEnd(); PI != PE; ++PI) { |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1187 | // Emit processor resource table. |
| 1188 | if (PI->hasInstrSchedModel()) |
| 1189 | EmitProcessorResources(*PI, OS); |
| 1190 | else if(!PI->ProcResourceDefs.empty()) |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1191 | PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines " |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 1192 | "ProcResources without defining WriteRes SchedWriteRes"); |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1193 | |
Andrew Trick | fc99299 | 2012-06-05 03:44:40 +0000 | [diff] [blame] | 1194 | // Begin processor itinerary properties |
| 1195 | OS << "\n"; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1196 | OS << "static const llvm::MCSchedModel " << PI->ModelName << "(\n"; |
| 1197 | EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ','); |
| 1198 | EmitProcessorProp(OS, PI->ModelDef, "MinLatency", ','); |
| 1199 | EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ','); |
| 1200 | EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ','); |
Andrew Trick | 47579cf | 2013-01-09 03:36:49 +0000 | [diff] [blame] | 1201 | EmitProcessorProp(OS, PI->ModelDef, "ILPWindow", ','); |
Andrew Trick | d43b5c9 | 2012-08-08 02:44:16 +0000 | [diff] [blame] | 1202 | EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ','); |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 1203 | OS << " " << PI->Index << ", // Processor ID\n"; |
| 1204 | if (PI->hasInstrSchedModel()) |
| 1205 | OS << " " << PI->ModelName << "ProcResources" << ",\n" |
| 1206 | << " " << PI->ModelName << "SchedClasses" << ",\n" |
| 1207 | << " " << PI->ProcResourceDefs.size()+1 << ",\n" |
| 1208 | << " " << (SchedModels.schedClassEnd() |
| 1209 | - SchedModels.schedClassBegin()) << ",\n"; |
| 1210 | else |
| 1211 | OS << " 0, 0, 0, 0, // No instruction-level machine model.\n"; |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1212 | if (SchedModels.hasItineraries()) |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1213 | OS << " " << PI->ItinsDef->getName() << ");\n"; |
Andrew Trick | d85934b | 2012-06-22 03:58:51 +0000 | [diff] [blame] | 1214 | else |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1215 | OS << " 0); // No Itinerary\n"; |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 1216 | } |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | // |
| 1220 | // EmitProcessorLookup - generate cpu name to itinerary lookup table. |
| 1221 | // |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1222 | void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) { |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1223 | // Gather and sort processor information |
| 1224 | std::vector<Record*> ProcessorList = |
| 1225 | Records.getAllDerivedDefinitions("Processor"); |
Duraid Madina | 42d24c7 | 2005-12-30 14:56:37 +0000 | [diff] [blame] | 1226 | std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName()); |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Begin processor table |
| 1229 | OS << "\n"; |
| 1230 | OS << "// Sorted (by key) array of itineraries for CPU subtype.\n" |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 1231 | << "extern const llvm::SubtargetInfoKV " |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1232 | << Target << "ProcSchedKV[] = {\n"; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 1233 | |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1234 | // For each processor |
| 1235 | for (unsigned i = 0, N = ProcessorList.size(); i < N;) { |
| 1236 | // Next processor |
| 1237 | Record *Processor = ProcessorList[i]; |
| 1238 | |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 1239 | const std::string &Name = Processor->getValueAsString("Name"); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1240 | const std::string &ProcModelName = |
Andrew Trick | 48605c3 | 2012-09-15 00:19:57 +0000 | [diff] [blame] | 1241 | SchedModels.getModelForProc(Processor).ModelName; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 1242 | |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1243 | // Emit as { "cpu", procinit }, |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1244 | OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }"; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 1245 | |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1246 | // Depending on ''if more in the list'' emit comma |
| 1247 | if (++i < N) OS << ","; |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 1248 | |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1249 | OS << "\n"; |
| 1250 | } |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 1251 | |
Jim Laskey | 10b1dd9 | 2005-10-31 17:16:01 +0000 | [diff] [blame] | 1252 | // End processor table |
| 1253 | OS << "};\n"; |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | // |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1257 | // EmitSchedModel - Emits all scheduling model tables, folding common patterns. |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 1258 | // |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1259 | void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) { |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1260 | OS << "#ifdef DBGFIELD\n" |
| 1261 | << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n" |
| 1262 | << "#endif\n" |
| 1263 | << "#ifndef NDEBUG\n" |
| 1264 | << "#define DBGFIELD(x) x,\n" |
| 1265 | << "#else\n" |
| 1266 | << "#define DBGFIELD(x)\n" |
| 1267 | << "#endif\n"; |
| 1268 | |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1269 | if (SchedModels.hasItineraries()) { |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1270 | std::vector<std::vector<InstrItinerary> > ProcItinLists; |
Jim Laskey | 6cee630 | 2005-11-01 20:06:59 +0000 | [diff] [blame] | 1271 | // Emit the stage data |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1272 | EmitStageAndOperandCycleData(OS, ProcItinLists); |
| 1273 | EmitItineraries(OS, ProcItinLists); |
Jim Laskey | 6cee630 | 2005-11-01 20:06:59 +0000 | [diff] [blame] | 1274 | } |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1275 | OS << "\n// ===============================================================\n" |
| 1276 | << "// Data tables for the new per-operand machine model.\n"; |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1277 | |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 1278 | SchedClassTables SchedTables; |
| 1279 | for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), |
| 1280 | PE = SchedModels.procModelEnd(); PI != PE; ++PI) { |
| 1281 | GenSchedClassTables(*PI, SchedTables); |
| 1282 | } |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1283 | EmitSchedClassTables(SchedTables, OS); |
| 1284 | |
| 1285 | // Emit the processor machine model |
| 1286 | EmitProcessorModels(OS); |
| 1287 | // Emit the processor lookup data |
| 1288 | EmitProcessorLookup(OS); |
Andrew Trick | 52c3a1d | 2012-09-17 22:18:48 +0000 | [diff] [blame] | 1289 | |
Andrew Trick | 40096d2 | 2012-09-17 22:18:45 +0000 | [diff] [blame] | 1290 | OS << "#undef DBGFIELD"; |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Andrew Trick | 4d2d1c4 | 2012-09-18 03:41:43 +0000 | [diff] [blame] | 1293 | void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName, |
| 1294 | raw_ostream &OS) { |
| 1295 | OS << "unsigned " << ClassName |
| 1296 | << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI," |
| 1297 | << " const TargetSchedModel *SchedModel) const {\n"; |
| 1298 | |
| 1299 | std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog"); |
| 1300 | std::sort(Prologs.begin(), Prologs.end(), LessRecord()); |
| 1301 | for (std::vector<Record*>::const_iterator |
| 1302 | PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) { |
| 1303 | OS << (*PI)->getValueAsString("Code") << '\n'; |
| 1304 | } |
| 1305 | IdxVec VariantClasses; |
| 1306 | for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(), |
| 1307 | SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) { |
| 1308 | if (SCI->Transitions.empty()) |
| 1309 | continue; |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1310 | VariantClasses.push_back(SCI->Index); |
Andrew Trick | 4d2d1c4 | 2012-09-18 03:41:43 +0000 | [diff] [blame] | 1311 | } |
| 1312 | if (!VariantClasses.empty()) { |
| 1313 | OS << " switch (SchedClass) {\n"; |
| 1314 | for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end(); |
| 1315 | VCI != VCE; ++VCI) { |
| 1316 | const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI); |
| 1317 | OS << " case " << *VCI << ": // " << SC.Name << '\n'; |
| 1318 | IdxVec ProcIndices; |
| 1319 | for (std::vector<CodeGenSchedTransition>::const_iterator |
| 1320 | TI = SC.Transitions.begin(), TE = SC.Transitions.end(); |
| 1321 | TI != TE; ++TI) { |
| 1322 | IdxVec PI; |
| 1323 | std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(), |
| 1324 | ProcIndices.begin(), ProcIndices.end(), |
| 1325 | std::back_inserter(PI)); |
| 1326 | ProcIndices.swap(PI); |
| 1327 | } |
| 1328 | for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end(); |
| 1329 | PI != PE; ++PI) { |
| 1330 | OS << " "; |
| 1331 | if (*PI != 0) |
| 1332 | OS << "if (SchedModel->getProcessorID() == " << *PI << ") "; |
| 1333 | OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName |
| 1334 | << '\n'; |
| 1335 | for (std::vector<CodeGenSchedTransition>::const_iterator |
| 1336 | TI = SC.Transitions.begin(), TE = SC.Transitions.end(); |
| 1337 | TI != TE; ++TI) { |
| 1338 | OS << " if ("; |
| 1339 | if (*PI != 0 && !std::count(TI->ProcIndices.begin(), |
| 1340 | TI->ProcIndices.end(), *PI)) { |
| 1341 | continue; |
| 1342 | } |
| 1343 | for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end(); |
| 1344 | RI != RE; ++RI) { |
| 1345 | if (RI != TI->PredTerm.begin()) |
| 1346 | OS << "\n && "; |
| 1347 | OS << "(" << (*RI)->getValueAsString("Predicate") << ")"; |
| 1348 | } |
| 1349 | OS << ")\n" |
| 1350 | << " return " << TI->ToClassIdx << "; // " |
| 1351 | << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n'; |
| 1352 | } |
| 1353 | OS << " }\n"; |
| 1354 | if (*PI == 0) |
| 1355 | break; |
| 1356 | } |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1357 | if (SC.isInferred()) |
| 1358 | OS << " return " << SC.Index << ";\n"; |
Andrew Trick | 4d2d1c4 | 2012-09-18 03:41:43 +0000 | [diff] [blame] | 1359 | OS << " break;\n"; |
| 1360 | } |
| 1361 | OS << " };\n"; |
| 1362 | } |
| 1363 | OS << " report_fatal_error(\"Expected a variant SchedClass\");\n" |
| 1364 | << "} // " << ClassName << "::resolveSchedClass\n"; |
| 1365 | } |
| 1366 | |
Jim Laskey | 0d841e0 | 2005-10-27 19:47:21 +0000 | [diff] [blame] | 1367 | // |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 1368 | // ParseFeaturesFunction - Produces a subtarget specific function for parsing |
| 1369 | // the subtarget features string. |
| 1370 | // |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1371 | void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS, |
| 1372 | unsigned NumFeatures, |
| 1373 | unsigned NumProcs) { |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 1374 | std::vector<Record*> Features = |
| 1375 | Records.getAllDerivedDefinitions("SubtargetFeature"); |
Duraid Madina | 42d24c7 | 2005-12-30 14:56:37 +0000 | [diff] [blame] | 1376 | std::sort(Features.begin(), Features.end(), LessRecord()); |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 1377 | |
Andrew Trick | da96cf2 | 2011-04-01 01:56:55 +0000 | [diff] [blame] | 1378 | OS << "// ParseSubtargetFeatures - Parses features string setting specified\n" |
| 1379 | << "// subtarget options.\n" |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 1380 | << "void llvm::"; |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 1381 | OS << Target; |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 1382 | OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n" |
David Greene | f0fd3af | 2010-01-05 17:47:41 +0000 | [diff] [blame] | 1383 | << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n" |
Hal Finkel | 3f696e5 | 2012-06-12 04:21:36 +0000 | [diff] [blame] | 1384 | << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n"; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1385 | |
| 1386 | if (Features.empty()) { |
| 1387 | OS << "}\n"; |
| 1388 | return; |
| 1389 | } |
| 1390 | |
Andrew Trick | 34aadd6 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 1391 | OS << " InitMCProcessorInfo(CPU, FS);\n" |
| 1392 | << " uint64_t Bits = getFeatureBits();\n"; |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 1393 | |
Jim Laskey | f7bcde0 | 2005-10-28 21:47:29 +0000 | [diff] [blame] | 1394 | for (unsigned i = 0; i < Features.size(); i++) { |
| 1395 | // Next record |
| 1396 | Record *R = Features[i]; |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 1397 | const std::string &Instance = R->getName(); |
| 1398 | const std::string &Value = R->getValueAsString("Value"); |
| 1399 | const std::string &Attribute = R->getValueAsString("Attribute"); |
Evan Cheng | 19c9550 | 2006-01-27 08:09:42 +0000 | [diff] [blame] | 1400 | |
Dale Johannesen | db01c8b | 2008-02-14 23:35:16 +0000 | [diff] [blame] | 1401 | if (Value=="true" || Value=="false") |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 1402 | OS << " if ((Bits & " << Target << "::" |
| 1403 | << Instance << ") != 0) " |
Dale Johannesen | db01c8b | 2008-02-14 23:35:16 +0000 | [diff] [blame] | 1404 | << Attribute << " = " << Value << ";\n"; |
| 1405 | else |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 1406 | OS << " if ((Bits & " << Target << "::" |
| 1407 | << Instance << ") != 0 && " |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1408 | << Attribute << " < " << Value << ") " |
| 1409 | << Attribute << " = " << Value << ";\n"; |
Jim Laskey | 6cee630 | 2005-11-01 20:06:59 +0000 | [diff] [blame] | 1410 | } |
Anton Korobeynikov | 41a0243 | 2009-05-23 19:50:50 +0000 | [diff] [blame] | 1411 | |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 1412 | OS << "}\n"; |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Anton Korobeynikov | 41a0243 | 2009-05-23 19:50:50 +0000 | [diff] [blame] | 1415 | // |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 1416 | // SubtargetEmitter::run - Main subtarget enumeration emitter. |
| 1417 | // |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1418 | void SubtargetEmitter::run(raw_ostream &OS) { |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 1419 | emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS); |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 1420 | |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 1421 | OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n"; |
| 1422 | OS << "#undef GET_SUBTARGETINFO_ENUM\n"; |
| 1423 | |
| 1424 | OS << "namespace llvm {\n"; |
| 1425 | Enumeration(OS, "SubtargetFeature", true); |
| 1426 | OS << "} // End llvm namespace \n"; |
| 1427 | OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n"; |
| 1428 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1429 | OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n"; |
| 1430 | OS << "#undef GET_SUBTARGETINFO_MC_DESC\n"; |
Anton Korobeynikov | 928eb49 | 2010-04-18 20:31:01 +0000 | [diff] [blame] | 1431 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1432 | OS << "namespace llvm {\n"; |
Evan Cheng | c60f9b7 | 2011-07-14 20:59:42 +0000 | [diff] [blame] | 1433 | #if 0 |
| 1434 | OS << "namespace {\n"; |
| 1435 | #endif |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1436 | unsigned NumFeatures = FeatureKeyValues(OS); |
Evan Cheng | c60f9b7 | 2011-07-14 20:59:42 +0000 | [diff] [blame] | 1437 | OS << "\n"; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1438 | unsigned NumProcs = CPUKeyValues(OS); |
Evan Cheng | c60f9b7 | 2011-07-14 20:59:42 +0000 | [diff] [blame] | 1439 | OS << "\n"; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1440 | EmitSchedModel(OS); |
Evan Cheng | c60f9b7 | 2011-07-14 20:59:42 +0000 | [diff] [blame] | 1441 | OS << "\n"; |
| 1442 | #if 0 |
| 1443 | OS << "}\n"; |
| 1444 | #endif |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1445 | |
| 1446 | // MCInstrInfo initialization routine. |
| 1447 | OS << "static inline void Init" << Target |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 1448 | << "MCSubtargetInfo(MCSubtargetInfo *II, " |
| 1449 | << "StringRef TT, StringRef CPU, StringRef FS) {\n"; |
| 1450 | OS << " II->InitMCSubtargetInfo(TT, CPU, FS, "; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1451 | if (NumFeatures) |
| 1452 | OS << Target << "FeatureKV, "; |
| 1453 | else |
| 1454 | OS << "0, "; |
| 1455 | if (NumProcs) |
| 1456 | OS << Target << "SubTypeKV, "; |
| 1457 | else |
| 1458 | OS << "0, "; |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1459 | OS << '\n'; OS.indent(22); |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 1460 | OS << Target << "ProcSchedKV, " |
| 1461 | << Target << "WriteProcResTable, " |
| 1462 | << Target << "WriteLatencyTable, " |
| 1463 | << Target << "ReadAdvanceTable, "; |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1464 | if (SchedModels.hasItineraries()) { |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 1465 | OS << '\n'; OS.indent(22); |
| 1466 | OS << Target << "Stages, " |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1467 | << Target << "OperandCycles, " |
Andrew Trick | a11a628 | 2012-07-07 03:59:48 +0000 | [diff] [blame] | 1468 | << Target << "ForwardingPaths, "; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1469 | } else |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 1470 | OS << "0, 0, 0, "; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1471 | OS << NumFeatures << ", " << NumProcs << ");\n}\n\n"; |
| 1472 | |
| 1473 | OS << "} // End llvm namespace \n"; |
| 1474 | |
| 1475 | OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n"; |
| 1476 | |
| 1477 | OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n"; |
| 1478 | OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n"; |
| 1479 | |
| 1480 | OS << "#include \"llvm/Support/Debug.h\"\n"; |
| 1481 | OS << "#include \"llvm/Support/raw_ostream.h\"\n"; |
| 1482 | ParseFeaturesFunction(OS, NumFeatures, NumProcs); |
| 1483 | |
| 1484 | OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n"; |
| 1485 | |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 1486 | // Create a TargetSubtargetInfo subclass to hide the MC layer initialization. |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1487 | OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n"; |
| 1488 | OS << "#undef GET_SUBTARGETINFO_HEADER\n"; |
| 1489 | |
| 1490 | std::string ClassName = Target + "GenSubtargetInfo"; |
| 1491 | OS << "namespace llvm {\n"; |
Anshuman Dasgupta | dc81e5d | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 1492 | OS << "class DFAPacketizer;\n"; |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 1493 | OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n" |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 1494 | << " explicit " << ClassName << "(StringRef TT, StringRef CPU, " |
| 1495 | << "StringRef FS);\n" |
Anshuman Dasgupta | dc81e5d | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 1496 | << "public:\n" |
Andrew Trick | 4d2d1c4 | 2012-09-18 03:41:43 +0000 | [diff] [blame] | 1497 | << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI," |
| 1498 | << " const TargetSchedModel *SchedModel) const;\n" |
Sebastian Pop | 464f3a3 | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 1499 | << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)" |
Anshuman Dasgupta | dc81e5d | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 1500 | << " const;\n" |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1501 | << "};\n"; |
| 1502 | OS << "} // End llvm namespace \n"; |
| 1503 | |
| 1504 | OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n"; |
| 1505 | |
| 1506 | OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n"; |
| 1507 | OS << "#undef GET_SUBTARGETINFO_CTOR\n"; |
| 1508 | |
Andrew Trick | ee290ba | 2012-09-18 03:32:57 +0000 | [diff] [blame] | 1509 | OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n"; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1510 | OS << "namespace llvm {\n"; |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 1511 | OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n"; |
| 1512 | OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n"; |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1513 | OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n"; |
| 1514 | OS << "extern const llvm::MCWriteProcResEntry " |
| 1515 | << Target << "WriteProcResTable[];\n"; |
| 1516 | OS << "extern const llvm::MCWriteLatencyEntry " |
| 1517 | << Target << "WriteLatencyTable[];\n"; |
| 1518 | OS << "extern const llvm::MCReadAdvanceEntry " |
| 1519 | << Target << "ReadAdvanceTable[];\n"; |
| 1520 | |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1521 | if (SchedModels.hasItineraries()) { |
Benjamin Kramer | 1a2f988 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 1522 | OS << "extern const llvm::InstrStage " << Target << "Stages[];\n"; |
| 1523 | OS << "extern const unsigned " << Target << "OperandCycles[];\n"; |
Andrew Trick | a11a628 | 2012-07-07 03:59:48 +0000 | [diff] [blame] | 1524 | OS << "extern const unsigned " << Target << "ForwardingPaths[];\n"; |
Evan Cheng | c60f9b7 | 2011-07-14 20:59:42 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 1527 | OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, " |
| 1528 | << "StringRef FS)\n" |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 1529 | << " : TargetSubtargetInfo() {\n" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 1530 | << " InitMCSubtargetInfo(TT, CPU, FS, "; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1531 | if (NumFeatures) |
| 1532 | OS << Target << "FeatureKV, "; |
| 1533 | else |
| 1534 | OS << "0, "; |
| 1535 | if (NumProcs) |
| 1536 | OS << Target << "SubTypeKV, "; |
| 1537 | else |
| 1538 | OS << "0, "; |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 1539 | OS << '\n'; OS.indent(22); |
| 1540 | OS << Target << "ProcSchedKV, " |
| 1541 | << Target << "WriteProcResTable, " |
| 1542 | << Target << "WriteLatencyTable, " |
| 1543 | << Target << "ReadAdvanceTable, "; |
| 1544 | OS << '\n'; OS.indent(22); |
Andrew Trick | 1ab961f | 2013-03-16 18:58:55 +0000 | [diff] [blame] | 1545 | if (SchedModels.hasItineraries()) { |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 1546 | OS << Target << "Stages, " |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1547 | << Target << "OperandCycles, " |
Andrew Trick | a11a628 | 2012-07-07 03:59:48 +0000 | [diff] [blame] | 1548 | << Target << "ForwardingPaths, "; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1549 | } else |
Andrew Trick | e127dfd | 2012-09-18 03:18:56 +0000 | [diff] [blame] | 1550 | OS << "0, 0, 0, "; |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1551 | OS << NumFeatures << ", " << NumProcs << ");\n}\n\n"; |
Andrew Trick | 544c880 | 2012-09-17 22:18:50 +0000 | [diff] [blame] | 1552 | |
Andrew Trick | 4d2d1c4 | 2012-09-18 03:41:43 +0000 | [diff] [blame] | 1553 | EmitSchedModelHelpers(ClassName, OS); |
| 1554 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 1555 | OS << "} // End llvm namespace \n"; |
| 1556 | |
| 1557 | OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n"; |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 1558 | } |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 1559 | |
| 1560 | namespace llvm { |
| 1561 | |
| 1562 | void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) { |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 1563 | CodeGenTarget CGTarget(RK); |
| 1564 | SubtargetEmitter(RK, CGTarget).run(OS); |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | } // End llvm namespace |