1 //===- MCSubtargetInfo.cpp - Subtarget Information ------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/MC/MCSubtargetInfo.h" 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/StringRef.h" 12 #include "llvm/MC/MCInstrItineraries.h" 13 #include "llvm/MC/MCSchedule.h" 14 #include "llvm/Support/Format.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include "llvm/TargetParser/SubtargetFeature.h" 17 #include <algorithm> 18 #include <cassert> 19 #include <cstring> 20 #include <optional> 21 22 using namespace llvm; 23 24 /// Find KV in array using binary search. 25 template <typename T> 26 static const T *Find(StringRef S, ArrayRef<T> A) { 27 // Binary search the array 28 auto F = llvm::lower_bound(A, S); 29 // If not found then return NULL 30 if (F == A.end() || StringRef(F->Key) != S) return nullptr; 31 // Return the found array item 32 return F; 33 } 34 35 /// For each feature that is (transitively) implied by this feature, set it. 36 static 37 void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies, 38 ArrayRef<SubtargetFeatureKV> FeatureTable) { 39 // OR the Implies bits in outside the loop. This allows the Implies for CPUs 40 // which might imply features not in FeatureTable to use this. 41 Bits |= Implies; 42 for (const SubtargetFeatureKV &FE : FeatureTable) 43 if (Implies.test(FE.Value)) 44 SetImpliedBits(Bits, FE.Implies.getAsBitset(), FeatureTable); 45 } 46 47 /// For each feature that (transitively) implies this feature, clear it. 48 static 49 void ClearImpliedBits(FeatureBitset &Bits, unsigned Value, 50 ArrayRef<SubtargetFeatureKV> FeatureTable) { 51 for (const SubtargetFeatureKV &FE : FeatureTable) { 52 if (FE.Implies.getAsBitset().test(Value)) { 53 Bits.reset(FE.Value); 54 ClearImpliedBits(Bits, FE.Value, FeatureTable); 55 } 56 } 57 } 58 59 static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature, 60 ArrayRef<SubtargetFeatureKV> FeatureTable) { 61 assert(SubtargetFeatures::hasFlag(Feature) && 62 "Feature flags should start with '+' or '-'"); 63 64 // Find feature in table. 65 const SubtargetFeatureKV *FeatureEntry = 66 Find(SubtargetFeatures::StripFlag(Feature), FeatureTable); 67 // If there is a match 68 if (FeatureEntry) { 69 // Enable/disable feature in bits 70 if (SubtargetFeatures::isEnabled(Feature)) { 71 Bits.set(FeatureEntry->Value); 72 73 // For each feature that this implies, set it. 74 SetImpliedBits(Bits, FeatureEntry->Implies.getAsBitset(), FeatureTable); 75 } else { 76 Bits.reset(FeatureEntry->Value); 77 78 // For each feature that implies this, clear it. 79 ClearImpliedBits(Bits, FeatureEntry->Value, FeatureTable); 80 } 81 } else { 82 errs() << "'" << Feature << "' is not a recognized feature for this target" 83 << " (ignoring feature)\n"; 84 } 85 } 86 87 /// Return the length of the longest entry in the table. 88 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) { 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 static size_t getLongestEntryLength(ArrayRef<StringRef> Table) { 96 size_t MaxLen = 0; 97 for (StringRef I : Table) 98 MaxLen = std::max(MaxLen, I.size()); 99 return MaxLen; 100 } 101 102 /// Display help for feature and mcpu choices. 103 static void Help(ArrayRef<StringRef> CPUNames, 104 ArrayRef<SubtargetFeatureKV> FeatTable) { 105 // the static variable ensures that the help information only gets 106 // printed once even though a target machine creates multiple subtargets 107 static bool PrintOnce = false; 108 if (PrintOnce) { 109 return; 110 } 111 112 // Determine the length of the longest CPU and Feature entries. 113 unsigned MaxCPULen = getLongestEntryLength(CPUNames); 114 unsigned MaxFeatLen = getLongestEntryLength(FeatTable); 115 116 // Print the CPU table. 117 errs() << "Available CPUs for this target:\n\n"; 118 for (auto &CPUName : CPUNames) { 119 // Skip apple-latest, as that's only meant to be used in 120 // disassemblers/debuggers, and we don't want normal code to be built with 121 // it as an -mcpu= 122 if (CPUName == "apple-latest") 123 continue; 124 errs() << format(" %-*s - Select the %s processor.\n", MaxCPULen, 125 CPUName.str().c_str(), CPUName.str().c_str()); 126 } 127 errs() << '\n'; 128 129 // Print the Feature table. 130 errs() << "Available features for this target:\n\n"; 131 for (auto &Feature : FeatTable) 132 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc); 133 errs() << '\n'; 134 135 errs() << "Use +feature to enable a feature, or -feature to disable it.\n" 136 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; 137 138 PrintOnce = true; 139 } 140 141 /// Display help for mcpu choices only 142 static void cpuHelp(ArrayRef<StringRef> CPUNames) { 143 // the static variable ensures that the help information only gets 144 // printed once even though a target machine creates multiple subtargets 145 static bool PrintOnce = false; 146 if (PrintOnce) { 147 return; 148 } 149 150 // Print the CPU table. 151 errs() << "Available CPUs for this target:\n\n"; 152 for (auto &CPU : CPUNames) { 153 // Skip apple-latest, as that's only meant to be used in 154 // disassemblers/debuggers, and we don't want normal code to be built with 155 // it as an -mcpu= 156 if (CPU == "apple-latest") 157 continue; 158 errs() << "\t" << CPU << "\n"; 159 } 160 errs() << '\n'; 161 162 errs() << "Use -mcpu or -mtune to specify the target's processor.\n" 163 "For example, clang --target=aarch64-unknown-linux-gnu " 164 "-mcpu=cortex-a35\n"; 165 166 PrintOnce = true; 167 } 168 169 static FeatureBitset getFeatures(MCSubtargetInfo &STI, StringRef CPU, 170 StringRef TuneCPU, StringRef FS, 171 ArrayRef<StringRef> ProcNames, 172 ArrayRef<SubtargetSubTypeKV> ProcDesc, 173 ArrayRef<SubtargetFeatureKV> ProcFeatures) { 174 SubtargetFeatures Features(FS); 175 176 if (ProcDesc.empty() || ProcFeatures.empty()) 177 return FeatureBitset(); 178 179 assert(llvm::is_sorted(ProcDesc) && "CPU table is not sorted"); 180 assert(llvm::is_sorted(ProcFeatures) && "CPU features table is not sorted"); 181 // Resulting bits 182 FeatureBitset Bits; 183 184 // Check if help is needed 185 if (CPU == "help") 186 Help(ProcNames, ProcFeatures); 187 188 // Find CPU entry if CPU name is specified. 189 else if (!CPU.empty()) { 190 const SubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc); 191 192 // If there is a match 193 if (CPUEntry) { 194 // Set the features implied by this CPU feature, if any. 195 SetImpliedBits(Bits, CPUEntry->Implies.getAsBitset(), ProcFeatures); 196 } else { 197 errs() << "'" << CPU << "' is not a recognized processor for this target" 198 << " (ignoring processor)\n"; 199 } 200 } 201 202 if (!TuneCPU.empty()) { 203 const SubtargetSubTypeKV *CPUEntry = Find(TuneCPU, ProcDesc); 204 205 // If there is a match 206 if (CPUEntry) { 207 // Set the features implied by this CPU feature, if any. 208 SetImpliedBits(Bits, CPUEntry->TuneImplies.getAsBitset(), ProcFeatures); 209 } else if (TuneCPU != CPU) { 210 errs() << "'" << TuneCPU << "' is not a recognized processor for this " 211 << "target (ignoring processor)\n"; 212 } 213 } 214 215 // Iterate through each feature 216 for (const std::string &Feature : Features.getFeatures()) { 217 // Check for help 218 if (Feature == "+help") 219 Help(ProcNames, ProcFeatures); 220 else if (Feature == "+cpuhelp") 221 cpuHelp(ProcNames); 222 else 223 ApplyFeatureFlag(Bits, Feature, ProcFeatures); 224 } 225 226 return Bits; 227 } 228 229 void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU, 230 StringRef FS) { 231 FeatureBits = 232 getFeatures(*this, CPU, TuneCPU, FS, ProcNames, ProcDesc, ProcFeatures); 233 FeatureString = std::string(FS); 234 235 if (!TuneCPU.empty()) 236 CPUSchedModel = &getSchedModelForCPU(TuneCPU); 237 else 238 CPUSchedModel = &MCSchedModel::Default; 239 } 240 241 void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef TuneCPU, 242 StringRef FS) { 243 FeatureBits = 244 getFeatures(*this, CPU, TuneCPU, FS, ProcNames, ProcDesc, ProcFeatures); 245 FeatureString = std::string(FS); 246 } 247 248 MCSubtargetInfo::MCSubtargetInfo( 249 const Triple &TT, StringRef C, StringRef TC, StringRef FS, 250 ArrayRef<StringRef> PN, ArrayRef<SubtargetFeatureKV> PF, 251 ArrayRef<SubtargetSubTypeKV> PD, const MCWriteProcResEntry *WPR, 252 const MCWriteLatencyEntry *WL, const MCReadAdvanceEntry *RA, 253 const InstrStage *IS, const unsigned *OC, const unsigned *FP) 254 : TargetTriple(TT), CPU(std::string(C)), TuneCPU(std::string(TC)), 255 ProcNames(PN), ProcFeatures(PF), ProcDesc(PD), WriteProcResTable(WPR), 256 WriteLatencyTable(WL), ReadAdvanceTable(RA), Stages(IS), 257 OperandCycles(OC), ForwardingPaths(FP) { 258 InitMCProcessorInfo(CPU, TuneCPU, FS); 259 } 260 261 FeatureBitset MCSubtargetInfo::ToggleFeature(uint64_t FB) { 262 FeatureBits.flip(FB); 263 return FeatureBits; 264 } 265 266 FeatureBitset MCSubtargetInfo::ToggleFeature(const FeatureBitset &FB) { 267 FeatureBits ^= FB; 268 return FeatureBits; 269 } 270 271 FeatureBitset MCSubtargetInfo::SetFeatureBitsTransitively( 272 const FeatureBitset &FB) { 273 SetImpliedBits(FeatureBits, FB, ProcFeatures); 274 return FeatureBits; 275 } 276 277 FeatureBitset MCSubtargetInfo::ClearFeatureBitsTransitively( 278 const FeatureBitset &FB) { 279 for (unsigned I = 0, E = FB.size(); I < E; I++) { 280 if (FB[I]) { 281 FeatureBits.reset(I); 282 ClearImpliedBits(FeatureBits, I, ProcFeatures); 283 } 284 } 285 return FeatureBits; 286 } 287 288 FeatureBitset MCSubtargetInfo::ToggleFeature(StringRef Feature) { 289 // Find feature in table. 290 const SubtargetFeatureKV *FeatureEntry = 291 Find(SubtargetFeatures::StripFlag(Feature), ProcFeatures); 292 // If there is a match 293 if (FeatureEntry) { 294 if (FeatureBits.test(FeatureEntry->Value)) { 295 FeatureBits.reset(FeatureEntry->Value); 296 // For each feature that implies this, clear it. 297 ClearImpliedBits(FeatureBits, FeatureEntry->Value, ProcFeatures); 298 } else { 299 FeatureBits.set(FeatureEntry->Value); 300 301 // For each feature that this implies, set it. 302 SetImpliedBits(FeatureBits, FeatureEntry->Implies.getAsBitset(), 303 ProcFeatures); 304 } 305 } else { 306 errs() << "'" << Feature << "' is not a recognized feature for this target" 307 << " (ignoring feature)\n"; 308 } 309 310 return FeatureBits; 311 } 312 313 FeatureBitset MCSubtargetInfo::ApplyFeatureFlag(StringRef FS) { 314 ::ApplyFeatureFlag(FeatureBits, FS, ProcFeatures); 315 return FeatureBits; 316 } 317 318 bool MCSubtargetInfo::checkFeatures(StringRef FS) const { 319 SubtargetFeatures T(FS); 320 FeatureBitset Set, All; 321 for (std::string F : T.getFeatures()) { 322 ::ApplyFeatureFlag(Set, F, ProcFeatures); 323 if (F[0] == '-') 324 F[0] = '+'; 325 ::ApplyFeatureFlag(All, F, ProcFeatures); 326 } 327 return (FeatureBits & All) == Set; 328 } 329 330 const MCSchedModel &MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const { 331 assert(llvm::is_sorted(ProcDesc) && 332 "Processor machine model table is not sorted"); 333 334 // Find entry 335 const SubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc); 336 337 if (!CPUEntry) { 338 if (CPU != "help") // Don't error if the user asked for help. 339 errs() << "'" << CPU 340 << "' is not a recognized processor for this target" 341 << " (ignoring processor)\n"; 342 return MCSchedModel::Default; 343 } 344 assert(CPUEntry->SchedModel && "Missing processor SchedModel value"); 345 return *CPUEntry->SchedModel; 346 } 347 348 InstrItineraryData 349 MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const { 350 const MCSchedModel &SchedModel = getSchedModelForCPU(CPU); 351 return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths); 352 } 353 354 void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const { 355 InstrItins = InstrItineraryData(getSchedModel(), Stages, OperandCycles, 356 ForwardingPaths); 357 } 358 359 std::vector<SubtargetFeatureKV> 360 MCSubtargetInfo::getEnabledProcessorFeatures() const { 361 std::vector<SubtargetFeatureKV> EnabledFeatures; 362 auto IsEnabled = [&](const SubtargetFeatureKV &FeatureKV) { 363 return FeatureBits.test(FeatureKV.Value); 364 }; 365 llvm::copy_if(ProcFeatures, std::back_inserter(EnabledFeatures), IsEnabled); 366 return EnabledFeatures; 367 } 368 369 std::optional<unsigned> MCSubtargetInfo::getCacheSize(unsigned Level) const { 370 return std::nullopt; 371 } 372 373 std::optional<unsigned> 374 MCSubtargetInfo::getCacheAssociativity(unsigned Level) const { 375 return std::nullopt; 376 } 377 378 std::optional<unsigned> 379 MCSubtargetInfo::getCacheLineSize(unsigned Level) const { 380 return std::nullopt; 381 } 382 383 unsigned MCSubtargetInfo::getPrefetchDistance() const { 384 return 0; 385 } 386 387 unsigned MCSubtargetInfo::getMaxPrefetchIterationsAhead() const { 388 return UINT_MAX; 389 } 390 391 bool MCSubtargetInfo::enableWritePrefetching() const { 392 return false; 393 } 394 395 unsigned MCSubtargetInfo::getMinPrefetchStride(unsigned NumMemAccesses, 396 unsigned NumStridedMemAccesses, 397 unsigned NumPrefetches, 398 bool HasCall) const { 399 return 1; 400 } 401 402 bool MCSubtargetInfo::shouldPrefetchAddressSpace(unsigned AS) const { 403 return !AS; 404 } 405