1 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the SubtargetFeature interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/MC/SubtargetFeature.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/Format.h" 17 #include "llvm/Support/raw_ostream.h" 18 #include <algorithm> 19 #include <cassert> 20 #include <cctype> 21 #include <cstdlib> 22 using namespace llvm; 23 24 //===----------------------------------------------------------------------===// 25 // Static Helper Functions 26 //===----------------------------------------------------------------------===// 27 28 /// hasFlag - Determine if a feature has a flag; '+' or '-' 29 /// 30 static inline bool hasFlag(StringRef Feature) { 31 assert(!Feature.empty() && "Empty string"); 32 // Get first character 33 char Ch = Feature[0]; 34 // Check if first character is '+' or '-' flag 35 return Ch == '+' || Ch =='-'; 36 } 37 38 /// StripFlag - Return string stripped of flag. 39 /// 40 static inline std::string StripFlag(StringRef Feature) { 41 return hasFlag(Feature) ? Feature.substr(1) : Feature; 42 } 43 44 /// isEnabled - Return true if enable flag; '+'. 45 /// 46 static inline bool isEnabled(StringRef Feature) { 47 assert(!Feature.empty() && "Empty string"); 48 // Get first character 49 char Ch = Feature[0]; 50 // Check if first character is '+' for enabled 51 return Ch == '+'; 52 } 53 54 /// Split - Splits a string of comma separated items in to a vector of strings. 55 /// 56 static void Split(std::vector<std::string> &V, StringRef S) { 57 SmallVector<StringRef, 3> Tmp; 58 S.split(Tmp, ",", -1, false /* KeepEmpty */); 59 V.assign(Tmp.begin(), Tmp.end()); 60 } 61 62 /// Join a vector of strings to a string with a comma separating each element. 63 /// 64 static std::string Join(const std::vector<std::string> &V) { 65 // Start with empty string. 66 std::string Result; 67 // If the vector is not empty 68 if (!V.empty()) { 69 // Start with the first feature 70 Result = V[0]; 71 // For each successive feature 72 for (size_t i = 1; i < V.size(); i++) { 73 // Add a comma 74 Result += ","; 75 // Add the feature 76 Result += V[i]; 77 } 78 } 79 // Return the features string 80 return Result; 81 } 82 83 /// Adding features. 84 void SubtargetFeatures::AddFeature(StringRef String, bool Enable) { 85 // Don't add empty features. 86 if (!String.empty()) 87 // Convert to lowercase, prepend flag if we don't already have a flag. 88 Features.push_back(hasFlag(String) ? String.lower() 89 : (Enable ? "+" : "-") + String.lower()); 90 } 91 92 /// Find KV in array using binary search. 93 static const SubtargetFeatureKV *Find(StringRef S, 94 ArrayRef<SubtargetFeatureKV> A) { 95 // Binary search the array 96 auto F = std::lower_bound(A.begin(), A.end(), S); 97 // If not found then return NULL 98 if (F == A.end() || StringRef(F->Key) != S) return nullptr; 99 // Return the found array item 100 return F; 101 } 102 103 /// getLongestEntryLength - Return the length of the longest entry in the table. 104 /// 105 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) { 106 size_t MaxLen = 0; 107 for (auto &I : Table) 108 MaxLen = std::max(MaxLen, std::strlen(I.Key)); 109 return MaxLen; 110 } 111 112 /// Display help for feature choices. 113 /// 114 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable, 115 ArrayRef<SubtargetFeatureKV> FeatTable) { 116 // Determine the length of the longest CPU and Feature entries. 117 unsigned MaxCPULen = getLongestEntryLength(CPUTable); 118 unsigned MaxFeatLen = getLongestEntryLength(FeatTable); 119 120 // Print the CPU table. 121 errs() << "Available CPUs for this target:\n\n"; 122 for (auto &CPU : CPUTable) 123 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc); 124 errs() << '\n'; 125 126 // Print the Feature table. 127 errs() << "Available features for this target:\n\n"; 128 for (auto &Feature : FeatTable) 129 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc); 130 errs() << '\n'; 131 132 errs() << "Use +feature to enable a feature, or -feature to disable it.\n" 133 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; 134 } 135 136 //===----------------------------------------------------------------------===// 137 // SubtargetFeatures Implementation 138 //===----------------------------------------------------------------------===// 139 140 SubtargetFeatures::SubtargetFeatures(StringRef Initial) { 141 // Break up string into separate features 142 Split(Features, Initial); 143 } 144 145 146 std::string SubtargetFeatures::getString() const { 147 return Join(Features); 148 } 149 150 /// SetImpliedBits - For each feature that is (transitively) implied by this 151 /// feature, set it. 152 /// 153 static 154 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry, 155 ArrayRef<SubtargetFeatureKV> FeatureTable) { 156 for (auto &FE : FeatureTable) { 157 if (FeatureEntry->Value == FE.Value) continue; 158 159 if (FeatureEntry->Implies & FE.Value) { 160 Bits |= FE.Value; 161 SetImpliedBits(Bits, &FE, FeatureTable); 162 } 163 } 164 } 165 166 /// ClearImpliedBits - For each feature that (transitively) implies this 167 /// feature, clear it. 168 /// 169 static 170 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry, 171 ArrayRef<SubtargetFeatureKV> FeatureTable) { 172 for (auto &FE : FeatureTable) { 173 if (FeatureEntry->Value == FE.Value) continue; 174 175 if (FE.Implies & FeatureEntry->Value) { 176 Bits &= ~FE.Value; 177 ClearImpliedBits(Bits, &FE, FeatureTable); 178 } 179 } 180 } 181 182 /// ToggleFeature - Toggle a feature and returns the newly updated feature 183 /// bits. 184 uint64_t 185 SubtargetFeatures::ToggleFeature(uint64_t Bits, StringRef Feature, 186 ArrayRef<SubtargetFeatureKV> FeatureTable) { 187 188 // Find feature in table. 189 const SubtargetFeatureKV *FeatureEntry = 190 Find(StripFlag(Feature), FeatureTable); 191 // If there is a match 192 if (FeatureEntry) { 193 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) { 194 Bits &= ~FeatureEntry->Value; 195 196 // For each feature that implies this, clear it. 197 ClearImpliedBits(Bits, FeatureEntry, FeatureTable); 198 } else { 199 Bits |= FeatureEntry->Value; 200 201 // For each feature that this implies, set it. 202 SetImpliedBits(Bits, FeatureEntry, FeatureTable); 203 } 204 } else { 205 errs() << "'" << Feature 206 << "' is not a recognized feature for this target" 207 << " (ignoring feature)\n"; 208 } 209 210 return Bits; 211 } 212 213 214 /// getFeatureBits - Get feature bits a CPU. 215 /// 216 uint64_t 217 SubtargetFeatures::getFeatureBits(StringRef CPU, 218 ArrayRef<SubtargetFeatureKV> CPUTable, 219 ArrayRef<SubtargetFeatureKV> FeatureTable) { 220 221 if (CPUTable.empty() || FeatureTable.empty()) 222 return 0; 223 224 #ifndef NDEBUG 225 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) { 226 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 && 227 "CPU table is not sorted"); 228 } 229 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) { 230 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 && 231 "CPU features table is not sorted"); 232 } 233 #endif 234 uint64_t Bits = 0; // Resulting bits 235 236 // Check if help is needed 237 if (CPU == "help") 238 Help(CPUTable, FeatureTable); 239 240 // Find CPU entry if CPU name is specified. 241 else if (!CPU.empty()) { 242 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable); 243 244 // If there is a match 245 if (CPUEntry) { 246 // Set base feature bits 247 Bits = CPUEntry->Value; 248 249 // Set the feature implied by this CPU feature, if any. 250 for (auto &FE : FeatureTable) { 251 if (CPUEntry->Value & FE.Value) 252 SetImpliedBits(Bits, &FE, FeatureTable); 253 } 254 } else { 255 errs() << "'" << CPU 256 << "' is not a recognized processor for this target" 257 << " (ignoring processor)\n"; 258 } 259 } 260 261 // Iterate through each feature 262 for (auto &Feature : Features) { 263 // Check for help 264 if (Feature == "+help") 265 Help(CPUTable, FeatureTable); 266 267 // Find feature in table. 268 const SubtargetFeatureKV *FeatureEntry = 269 Find(StripFlag(Feature), FeatureTable); 270 // If there is a match 271 if (FeatureEntry) { 272 // Enable/disable feature in bits 273 if (isEnabled(Feature)) { 274 Bits |= FeatureEntry->Value; 275 276 // For each feature that this implies, set it. 277 SetImpliedBits(Bits, FeatureEntry, FeatureTable); 278 } else { 279 Bits &= ~FeatureEntry->Value; 280 281 // For each feature that implies this, clear it. 282 ClearImpliedBits(Bits, FeatureEntry, FeatureTable); 283 } 284 } else { 285 errs() << "'" << Feature 286 << "' is not a recognized feature for this target" 287 << " (ignoring feature)\n"; 288 } 289 } 290 291 return Bits; 292 } 293 294 /// print - Print feature string. 295 /// 296 void SubtargetFeatures::print(raw_ostream &OS) const { 297 for (auto &F : Features) 298 OS << F << " "; 299 OS << "\n"; 300 } 301 302 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 303 /// dump - Dump feature info. 304 /// 305 void SubtargetFeatures::dump() const { 306 print(dbgs()); 307 } 308 #endif 309 310 /// Adds the default features for the specified target triple. 311 /// 312 /// FIXME: This is an inelegant way of specifying the features of a 313 /// subtarget. It would be better if we could encode this information 314 /// into the IR. See <rdar://5972456>. 315 /// 316 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) { 317 if (Triple.getVendor() == Triple::Apple) { 318 if (Triple.getArch() == Triple::ppc) { 319 // powerpc-apple-* 320 AddFeature("altivec"); 321 } else if (Triple.getArch() == Triple::ppc64) { 322 // powerpc64-apple-* 323 AddFeature("64bit"); 324 AddFeature("altivec"); 325 } 326 } 327 } 328