Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 1 | //===- MCSubtargetInfo.cpp - Subtarget Information ------------------------===// |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 9 | #include "llvm/MC/MCSubtargetInfo.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/ArrayRef.h" |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCInstrItineraries.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCSchedule.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/MC/SubtargetFeature.h" |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Format.h" |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include <algorithm> |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 18 | #include <cassert> |
| 19 | #include <cstring> |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 23 | /// Find KV in array using binary search. |
Craig Topper | ca26808 | 2019-03-05 18:54:34 +0000 | [diff] [blame] | 24 | template <typename T> |
| 25 | static const T *Find(StringRef S, ArrayRef<T> A) { |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 26 | // Binary search the array |
| 27 | auto F = std::lower_bound(A.begin(), A.end(), S); |
| 28 | // If not found then return NULL |
| 29 | if (F == A.end() || StringRef(F->Key) != S) return nullptr; |
| 30 | // Return the found array item |
| 31 | return F; |
| 32 | } |
| 33 | |
| 34 | /// For each feature that is (transitively) implied by this feature, set it. |
| 35 | static |
| 36 | void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies, |
| 37 | ArrayRef<SubtargetFeatureKV> FeatureTable) { |
| 38 | // OR the Implies bits in outside the loop. This allows the Implies for CPUs |
| 39 | // which might imply features not in FeatureTable to use this. |
| 40 | Bits |= Implies; |
| 41 | for (const SubtargetFeatureKV &FE : FeatureTable) |
| 42 | if (Implies.test(FE.Value)) |
| 43 | SetImpliedBits(Bits, FE.Implies.getAsBitset(), FeatureTable); |
| 44 | } |
| 45 | |
| 46 | /// For each feature that (transitively) implies this feature, clear it. |
| 47 | static |
| 48 | void ClearImpliedBits(FeatureBitset &Bits, unsigned Value, |
| 49 | ArrayRef<SubtargetFeatureKV> FeatureTable) { |
| 50 | for (const SubtargetFeatureKV &FE : FeatureTable) { |
| 51 | if (FE.Implies.getAsBitset().test(Value)) { |
| 52 | Bits.reset(FE.Value); |
| 53 | ClearImpliedBits(Bits, FE.Value, FeatureTable); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature, |
| 59 | ArrayRef<SubtargetFeatureKV> FeatureTable) { |
| 60 | assert(SubtargetFeatures::hasFlag(Feature) && |
| 61 | "Feature flags should start with '+' or '-'"); |
| 62 | |
| 63 | // Find feature in table. |
| 64 | const SubtargetFeatureKV *FeatureEntry = |
| 65 | Find(SubtargetFeatures::StripFlag(Feature), FeatureTable); |
| 66 | // If there is a match |
| 67 | if (FeatureEntry) { |
| 68 | // Enable/disable feature in bits |
| 69 | if (SubtargetFeatures::isEnabled(Feature)) { |
| 70 | Bits.set(FeatureEntry->Value); |
| 71 | |
| 72 | // For each feature that this implies, set it. |
| 73 | SetImpliedBits(Bits, FeatureEntry->Implies.getAsBitset(), FeatureTable); |
| 74 | } else { |
| 75 | Bits.reset(FeatureEntry->Value); |
| 76 | |
| 77 | // For each feature that implies this, clear it. |
| 78 | ClearImpliedBits(Bits, FeatureEntry->Value, FeatureTable); |
| 79 | } |
| 80 | } else { |
| 81 | errs() << "'" << Feature << "' is not a recognized feature for this target" |
| 82 | << " (ignoring feature)\n"; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Return the length of the longest entry in the table. |
Craig Topper | ca26808 | 2019-03-05 18:54:34 +0000 | [diff] [blame] | 87 | template <typename T> |
| 88 | static size_t getLongestEntryLength(ArrayRef<T> Table) { |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 89 | size_t MaxLen = 0; |
| 90 | for (auto &I : Table) |
| 91 | MaxLen = std::max(MaxLen, std::strlen(I.Key)); |
| 92 | return MaxLen; |
| 93 | } |
| 94 | |
| 95 | /// Display help for feature choices. |
Craig Topper | ca26808 | 2019-03-05 18:54:34 +0000 | [diff] [blame] | 96 | static void Help(ArrayRef<SubtargetSubTypeKV> CPUTable, |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 97 | ArrayRef<SubtargetFeatureKV> FeatTable) { |
| 98 | // Determine the length of the longest CPU and Feature entries. |
| 99 | unsigned MaxCPULen = getLongestEntryLength(CPUTable); |
| 100 | unsigned MaxFeatLen = getLongestEntryLength(FeatTable); |
| 101 | |
| 102 | // Print the CPU table. |
| 103 | errs() << "Available CPUs for this target:\n\n"; |
| 104 | for (auto &CPU : CPUTable) |
Craig Topper | ca26808 | 2019-03-05 18:54:34 +0000 | [diff] [blame] | 105 | errs() << format(" %-*s - Select the %s processor.\n", MaxCPULen, CPU.Key, |
| 106 | CPU.Key); |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 107 | errs() << '\n'; |
| 108 | |
| 109 | // Print the Feature table. |
| 110 | errs() << "Available features for this target:\n\n"; |
| 111 | for (auto &Feature : FeatTable) |
| 112 | errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc); |
| 113 | errs() << '\n'; |
| 114 | |
| 115 | errs() << "Use +feature to enable a feature, or -feature to disable it.\n" |
| 116 | "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; |
| 117 | } |
| 118 | |
Duncan P. N. Exon Smith | e463e47 | 2015-07-10 22:52:15 +0000 | [diff] [blame] | 119 | static FeatureBitset getFeatures(StringRef CPU, StringRef FS, |
Craig Topper | ca26808 | 2019-03-05 18:54:34 +0000 | [diff] [blame] | 120 | ArrayRef<SubtargetSubTypeKV> ProcDesc, |
Duncan P. N. Exon Smith | e463e47 | 2015-07-10 22:52:15 +0000 | [diff] [blame] | 121 | ArrayRef<SubtargetFeatureKV> ProcFeatures) { |
Andrew Trick | ba7b921 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 122 | SubtargetFeatures Features(FS); |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 123 | |
| 124 | if (ProcDesc.empty() || ProcFeatures.empty()) |
| 125 | return FeatureBitset(); |
| 126 | |
| 127 | assert(std::is_sorted(std::begin(ProcDesc), std::end(ProcDesc)) && |
| 128 | "CPU table is not sorted"); |
| 129 | assert(std::is_sorted(std::begin(ProcFeatures), std::end(ProcFeatures)) && |
| 130 | "CPU features table is not sorted"); |
| 131 | // Resulting bits |
| 132 | FeatureBitset Bits; |
| 133 | |
| 134 | // Check if help is needed |
| 135 | if (CPU == "help") |
| 136 | Help(ProcDesc, ProcFeatures); |
| 137 | |
| 138 | // Find CPU entry if CPU name is specified. |
| 139 | else if (!CPU.empty()) { |
Craig Topper | ca26808 | 2019-03-05 18:54:34 +0000 | [diff] [blame] | 140 | const SubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc); |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 141 | |
| 142 | // If there is a match |
| 143 | if (CPUEntry) { |
| 144 | // Set the features implied by this CPU feature, if any. |
| 145 | SetImpliedBits(Bits, CPUEntry->Implies.getAsBitset(), ProcFeatures); |
| 146 | } else { |
| 147 | errs() << "'" << CPU << "' is not a recognized processor for this target" |
| 148 | << " (ignoring processor)\n"; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Iterate through each feature |
| 153 | for (const std::string &Feature : Features.getFeatures()) { |
| 154 | // Check for help |
| 155 | if (Feature == "+help") |
| 156 | Help(ProcDesc, ProcFeatures); |
| 157 | else |
| 158 | ApplyFeatureFlag(Bits, Feature, ProcFeatures); |
| 159 | } |
| 160 | |
| 161 | return Bits; |
Duncan P. N. Exon Smith | e463e47 | 2015-07-10 22:52:15 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef FS) { |
| 165 | FeatureBits = getFeatures(CPU, FS, ProcDesc, ProcFeatures); |
Andrew Trick | ba7b921 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 166 | if (!CPU.empty()) |
Duncan P. N. Exon Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 167 | CPUSchedModel = &getSchedModelForCPU(CPU); |
Andrew Trick | ba7b921 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 168 | else |
Duncan P. N. Exon Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 169 | CPUSchedModel = &MCSchedModel::GetDefaultSchedModel(); |
Andrew Trick | ba7b921 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Bradley Smith | 323fee1 | 2015-11-16 11:10:19 +0000 | [diff] [blame] | 172 | void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef FS) { |
| 173 | FeatureBits = getFeatures(CPU, FS, ProcDesc, ProcFeatures); |
Duncan P. N. Exon Smith | e463e47 | 2015-07-10 22:52:15 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Duncan P. N. Exon Smith | 754e21f | 2015-07-10 22:43:42 +0000 | [diff] [blame] | 176 | MCSubtargetInfo::MCSubtargetInfo( |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 177 | const Triple &TT, StringRef C, StringRef FS, |
Craig Topper | ca26808 | 2019-03-05 18:54:34 +0000 | [diff] [blame] | 178 | ArrayRef<SubtargetFeatureKV> PF, ArrayRef<SubtargetSubTypeKV> PD, |
Craig Topper | 2982b84 | 2019-03-05 18:54:38 +0000 | [diff] [blame^] | 179 | const MCWriteProcResEntry *WPR, |
Daniel Sanders | a73f1fd | 2015-06-10 12:11:26 +0000 | [diff] [blame] | 180 | const MCWriteLatencyEntry *WL, const MCReadAdvanceEntry *RA, |
Duncan P. N. Exon Smith | 754e21f | 2015-07-10 22:43:42 +0000 | [diff] [blame] | 181 | const InstrStage *IS, const unsigned *OC, const unsigned *FP) |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 182 | : TargetTriple(TT), CPU(C), ProcFeatures(PF), ProcDesc(PD), |
Craig Topper | 2982b84 | 2019-03-05 18:54:38 +0000 | [diff] [blame^] | 183 | WriteProcResTable(WPR), WriteLatencyTable(WL), |
Duncan P. N. Exon Smith | 754e21f | 2015-07-10 22:43:42 +0000 | [diff] [blame] | 184 | ReadAdvanceTable(RA), Stages(IS), OperandCycles(OC), ForwardingPaths(FP) { |
Andrew Trick | ba7b921 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 185 | InitMCProcessorInfo(CPU, FS); |
Evan Cheng | 1a72add6 | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 188 | FeatureBitset MCSubtargetInfo::ToggleFeature(uint64_t FB) { |
| 189 | FeatureBits.flip(FB); |
| 190 | return FeatureBits; |
| 191 | } |
| 192 | |
| 193 | FeatureBitset MCSubtargetInfo::ToggleFeature(const FeatureBitset &FB) { |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 194 | FeatureBits ^= FB; |
| 195 | return FeatureBits; |
| 196 | } |
| 197 | |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 198 | FeatureBitset MCSubtargetInfo::ToggleFeature(StringRef Feature) { |
| 199 | // Find feature in table. |
| 200 | const SubtargetFeatureKV *FeatureEntry = |
| 201 | Find(SubtargetFeatures::StripFlag(Feature), ProcFeatures); |
| 202 | // If there is a match |
| 203 | if (FeatureEntry) { |
| 204 | if (FeatureBits.test(FeatureEntry->Value)) { |
| 205 | FeatureBits.reset(FeatureEntry->Value); |
| 206 | // For each feature that implies this, clear it. |
| 207 | ClearImpliedBits(FeatureBits, FeatureEntry->Value, ProcFeatures); |
| 208 | } else { |
| 209 | FeatureBits.set(FeatureEntry->Value); |
| 210 | |
| 211 | // For each feature that this implies, set it. |
| 212 | SetImpliedBits(FeatureBits, FeatureEntry->Implies.getAsBitset(), |
| 213 | ProcFeatures); |
| 214 | } |
| 215 | } else { |
| 216 | errs() << "'" << Feature << "' is not a recognized feature for this target" |
| 217 | << " (ignoring feature)\n"; |
| 218 | } |
| 219 | |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 220 | return FeatureBits; |
| 221 | } |
| 222 | |
John Brawn | d03d229 | 2015-06-05 13:29:24 +0000 | [diff] [blame] | 223 | FeatureBitset MCSubtargetInfo::ApplyFeatureFlag(StringRef FS) { |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 224 | ::ApplyFeatureFlag(FeatureBits, FS, ProcFeatures); |
John Brawn | d03d229 | 2015-06-05 13:29:24 +0000 | [diff] [blame] | 225 | return FeatureBits; |
| 226 | } |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 227 | |
Krzysztof Parzyszek | 788e768 | 2017-09-14 20:44:20 +0000 | [diff] [blame] | 228 | bool MCSubtargetInfo::checkFeatures(StringRef FS) const { |
| 229 | SubtargetFeatures T(FS); |
| 230 | FeatureBitset Set, All; |
| 231 | for (std::string F : T.getFeatures()) { |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 232 | ::ApplyFeatureFlag(Set, F, ProcFeatures); |
Krzysztof Parzyszek | 788e768 | 2017-09-14 20:44:20 +0000 | [diff] [blame] | 233 | if (F[0] == '-') |
| 234 | F[0] = '+'; |
Craig Topper | 16fc15a | 2019-03-05 18:54:30 +0000 | [diff] [blame] | 235 | ::ApplyFeatureFlag(All, F, ProcFeatures); |
Krzysztof Parzyszek | 788e768 | 2017-09-14 20:44:20 +0000 | [diff] [blame] | 236 | } |
| 237 | return (FeatureBits & All) == Set; |
| 238 | } |
| 239 | |
Duncan P. N. Exon Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 240 | const MCSchedModel &MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const { |
Craig Topper | 2982b84 | 2019-03-05 18:54:38 +0000 | [diff] [blame^] | 241 | assert(std::is_sorted(ProcDesc.begin(), ProcDesc.end()) && |
Craig Topper | c177d9e | 2015-10-17 16:37:11 +0000 | [diff] [blame] | 242 | "Processor machine model table is not sorted"); |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 243 | |
| 244 | // Find entry |
Craig Topper | 2982b84 | 2019-03-05 18:54:38 +0000 | [diff] [blame^] | 245 | const SubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc); |
| 246 | |
| 247 | if (!CPUEntry) { |
Craig Topper | 768ccc4 | 2015-04-02 04:27:50 +0000 | [diff] [blame] | 248 | if (CPU != "help") // Don't error if the user asked for help. |
| 249 | errs() << "'" << CPU |
| 250 | << "' is not a recognized processor for this target" |
| 251 | << " (ignoring processor)\n"; |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 252 | return MCSchedModel::GetDefaultSchedModel(); |
Artyom Skrobov | eab7515 | 2014-01-25 16:56:18 +0000 | [diff] [blame] | 253 | } |
Craig Topper | 2982b84 | 2019-03-05 18:54:38 +0000 | [diff] [blame^] | 254 | assert(CPUEntry->SchedModel && "Missing processor SchedModel value"); |
| 255 | return *CPUEntry->SchedModel; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 256 | } |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 257 | |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 258 | InstrItineraryData |
| 259 | MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const { |
Krzysztof Parzyszek | d0b6ceb | 2017-09-27 12:48:48 +0000 | [diff] [blame] | 260 | const MCSchedModel &SchedModel = getSchedModelForCPU(CPU); |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 261 | return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths); |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 262 | } |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 263 | |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 264 | void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const { |
Duncan P. N. Exon Smith | f862f87 | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 265 | InstrItins = InstrItineraryData(getSchedModel(), Stages, OperandCycles, |
| 266 | ForwardingPaths); |
Andrew Trick | d2a19da | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 267 | } |