1 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===// 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 /// \file Implements the SubtargetFeature interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/MC/SubtargetFeature.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/ADT/SmallVector.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/Config/llvm-config.h" 20 #include "llvm/Support/Compiler.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Support/Format.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <algorithm> 25 #include <cassert> 26 #include <cstddef> 27 #include <cstring> 28 #include <iterator> 29 #include <string> 30 #include <vector> 31 32 using namespace llvm; 33 34 /// Determine if a feature has a flag; '+' or '-' 35 static inline bool hasFlag(StringRef Feature) { 36 assert(!Feature.empty() && "Empty string"); 37 // Get first character 38 char Ch = Feature[0]; 39 // Check if first character is '+' or '-' flag 40 return Ch == '+' || Ch =='-'; 41 } 42 43 /// Return string stripped of flag. 44 static inline std::string StripFlag(StringRef Feature) { 45 return hasFlag(Feature) ? Feature.substr(1) : Feature; 46 } 47 48 /// Return true if enable flag; '+'. 49 static inline bool isEnabled(StringRef Feature) { 50 assert(!Feature.empty() && "Empty string"); 51 // Get first character 52 char Ch = Feature[0]; 53 // Check if first character is '+' for enabled 54 return Ch == '+'; 55 } 56 57 /// Splits a string of comma separated items in to a vector of strings. 58 static void Split(std::vector<std::string> &V, StringRef S) { 59 SmallVector<StringRef, 3> Tmp; 60 S.split(Tmp, ',', -1, false /* KeepEmpty */); 61 V.assign(Tmp.begin(), Tmp.end()); 62 } 63 64 void SubtargetFeatures::AddFeature(StringRef String, bool Enable) { 65 // Don't add empty features. 66 if (!String.empty()) 67 // Convert to lowercase, prepend flag if we don't already have a flag. 68 Features.push_back(hasFlag(String) ? String.lower() 69 : (Enable ? "+" : "-") + String.lower()); 70 } 71 72 /// Find KV in array using binary search. 73 static const SubtargetFeatureKV *Find(StringRef S, 74 ArrayRef<SubtargetFeatureKV> A) { 75 // Binary search the array 76 auto F = std::lower_bound(A.begin(), A.end(), S); 77 // If not found then return NULL 78 if (F == A.end() || StringRef(F->Key) != S) return nullptr; 79 // Return the found array item 80 return F; 81 } 82 83 /// Return the length of the longest entry in the table. 84 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) { 85 size_t MaxLen = 0; 86 for (auto &I : Table) 87 MaxLen = std::max(MaxLen, std::strlen(I.Key)); 88 return MaxLen; 89 } 90 91 /// Display help for feature choices. 92 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable, 93 ArrayRef<SubtargetFeatureKV> FeatTable) { 94 // Determine the length of the longest CPU and Feature entries. 95 unsigned MaxCPULen = getLongestEntryLength(CPUTable); 96 unsigned MaxFeatLen = getLongestEntryLength(FeatTable); 97 98 // Print the CPU table. 99 errs() << "Available CPUs for this target:\n\n"; 100 for (auto &CPU : CPUTable) 101 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc); 102 errs() << '\n'; 103 104 // Print the Feature table. 105 errs() << "Available features for this target:\n\n"; 106 for (auto &Feature : FeatTable) 107 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc); 108 errs() << '\n'; 109 110 errs() << "Use +feature to enable a feature, or -feature to disable it.\n" 111 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; 112 } 113 114 SubtargetFeatures::SubtargetFeatures(StringRef Initial) { 115 // Break up string into separate features 116 Split(Features, Initial); 117 } 118 119 std::string SubtargetFeatures::getString() const { 120 return join(Features.begin(), Features.end(), ","); 121 } 122 123 /// For each feature that is (transitively) implied by this feature, set it. 124 static 125 void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies, 126 ArrayRef<SubtargetFeatureKV> FeatureTable) { 127 // OR the Implies bits in outside the loop. This allows the Implies for CPUs 128 // which might imply features not in FeatureTable to use this. 129 Bits |= Implies; 130 for (const SubtargetFeatureKV &FE : FeatureTable) 131 if (Implies.test(FE.Value)) 132 SetImpliedBits(Bits, FE.Implies.getAsBitset(), FeatureTable); 133 } 134 135 /// For each feature that (transitively) implies this feature, clear it. 136 static 137 void ClearImpliedBits(FeatureBitset &Bits, unsigned Value, 138 ArrayRef<SubtargetFeatureKV> FeatureTable) { 139 for (const SubtargetFeatureKV &FE : FeatureTable) { 140 if (FE.Implies.getAsBitset().test(Value)) { 141 Bits.reset(FE.Value); 142 ClearImpliedBits(Bits, FE.Value, FeatureTable); 143 } 144 } 145 } 146 147 void 148 SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature, 149 ArrayRef<SubtargetFeatureKV> FeatureTable) { 150 // Find feature in table. 151 const SubtargetFeatureKV *FeatureEntry = 152 Find(StripFlag(Feature), FeatureTable); 153 // If there is a match 154 if (FeatureEntry) { 155 if (Bits.test(FeatureEntry->Value)) { 156 Bits.reset(FeatureEntry->Value); 157 // For each feature that implies this, clear it. 158 ClearImpliedBits(Bits, FeatureEntry->Value, FeatureTable); 159 } else { 160 Bits.set(FeatureEntry->Value); 161 162 // For each feature that this implies, set it. 163 SetImpliedBits(Bits, FeatureEntry->Implies.getAsBitset(), FeatureTable); 164 } 165 } else { 166 errs() << "'" << Feature << "' is not a recognized feature for this target" 167 << " (ignoring feature)\n"; 168 } 169 } 170 171 void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature, 172 ArrayRef<SubtargetFeatureKV> FeatureTable) { 173 assert(hasFlag(Feature)); 174 175 // Find feature in table. 176 const SubtargetFeatureKV *FeatureEntry = 177 Find(StripFlag(Feature), FeatureTable); 178 // If there is a match 179 if (FeatureEntry) { 180 // Enable/disable feature in bits 181 if (isEnabled(Feature)) { 182 Bits.set(FeatureEntry->Value); 183 184 // For each feature that this implies, set it. 185 SetImpliedBits(Bits, FeatureEntry->Implies.getAsBitset(), FeatureTable); 186 } else { 187 Bits.reset(FeatureEntry->Value); 188 189 // For each feature that implies this, clear it. 190 ClearImpliedBits(Bits, FeatureEntry->Value, FeatureTable); 191 } 192 } else { 193 errs() << "'" << Feature << "' is not a recognized feature for this target" 194 << " (ignoring feature)\n"; 195 } 196 } 197 198 FeatureBitset 199 SubtargetFeatures::getFeatureBits(StringRef CPU, 200 ArrayRef<SubtargetFeatureKV> CPUTable, 201 ArrayRef<SubtargetFeatureKV> FeatureTable) { 202 if (CPUTable.empty() || FeatureTable.empty()) 203 return FeatureBitset(); 204 205 assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) && 206 "CPU table is not sorted"); 207 assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) && 208 "CPU features table is not sorted"); 209 // Resulting bits 210 FeatureBitset Bits; 211 212 // Check if help is needed 213 if (CPU == "help") 214 Help(CPUTable, FeatureTable); 215 216 // Find CPU entry if CPU name is specified. 217 else if (!CPU.empty()) { 218 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable); 219 220 // If there is a match 221 if (CPUEntry) { 222 // Set the features implied by this CPU feature, if any. 223 SetImpliedBits(Bits, CPUEntry->Implies.getAsBitset(), FeatureTable); 224 } else { 225 errs() << "'" << CPU << "' is not a recognized processor for this target" 226 << " (ignoring processor)\n"; 227 } 228 } 229 230 // Iterate through each feature 231 for (const std::string &Feature : Features) { 232 // Check for help 233 if (Feature == "+help") 234 Help(CPUTable, FeatureTable); 235 else 236 ApplyFeatureFlag(Bits, Feature, FeatureTable); 237 } 238 239 return Bits; 240 } 241 242 void SubtargetFeatures::print(raw_ostream &OS) const { 243 for (auto &F : Features) 244 OS << F << " "; 245 OS << "\n"; 246 } 247 248 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 249 LLVM_DUMP_METHOD void SubtargetFeatures::dump() const { 250 print(dbgs()); 251 } 252 #endif 253 254 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) { 255 // FIXME: This is an inelegant way of specifying the features of a 256 // subtarget. It would be better if we could encode this information 257 // into the IR. See <rdar://5972456>. 258 if (Triple.getVendor() == Triple::Apple) { 259 if (Triple.getArch() == Triple::ppc) { 260 // powerpc-apple-* 261 AddFeature("altivec"); 262 } else if (Triple.getArch() == Triple::ppc64) { 263 // powerpc64-apple-* 264 AddFeature("64bit"); 265 AddFeature("altivec"); 266 } 267 } 268 } 269