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(const 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(const 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(const 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 /// PrependFlag - Return a string with a prepended flag; '+' or '-'. 55 /// 56 static inline std::string PrependFlag(const StringRef Feature, 57 bool IsEnabled) { 58 assert(!Feature.empty() && "Empty string"); 59 if (hasFlag(Feature)) 60 return Feature; 61 std::string Prefix = IsEnabled ? "+" : "-"; 62 Prefix += Feature; 63 return Prefix; 64 } 65 66 /// Split - Splits a string of comma separated items in to a vector of strings. 67 /// 68 static void Split(std::vector<std::string> &V, const StringRef S) { 69 if (S.empty()) 70 return; 71 72 // Start at beginning of string. 73 size_t Pos = 0; 74 while (true) { 75 // Find the next comma 76 size_t Comma = S.find(',', Pos); 77 // If no comma found then the rest of the string is used 78 if (Comma == std::string::npos) { 79 // Add string to vector 80 V.push_back(S.substr(Pos)); 81 break; 82 } 83 // Otherwise add substring to vector 84 V.push_back(S.substr(Pos, Comma - Pos)); 85 // Advance to next item 86 Pos = Comma + 1; 87 } 88 } 89 90 /// Join a vector of strings to a string with a comma separating each element. 91 /// 92 static std::string Join(const std::vector<std::string> &V) { 93 // Start with empty string. 94 std::string Result; 95 // If the vector is not empty 96 if (!V.empty()) { 97 // Start with the first feature 98 Result = V[0]; 99 // For each successive feature 100 for (size_t i = 1; i < V.size(); i++) { 101 // Add a comma 102 Result += ","; 103 // Add the feature 104 Result += V[i]; 105 } 106 } 107 // Return the features string 108 return Result; 109 } 110 111 /// Adding features. 112 void SubtargetFeatures::AddFeature(const StringRef String, 113 bool IsEnabled) { 114 // Don't add empty features 115 if (!String.empty()) { 116 // Convert to lowercase, prepend flag and add to vector 117 Features.push_back(PrependFlag(String.lower(), IsEnabled)); 118 } 119 } 120 121 /// Find KV in array using binary search. 122 static const SubtargetFeatureKV *Find(StringRef S, const SubtargetFeatureKV *A, 123 size_t L) { 124 // Determine the end of the array 125 const SubtargetFeatureKV *Hi = A + L; 126 // Binary search the array 127 const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S); 128 // If not found then return NULL 129 if (F == Hi || StringRef(F->Key) != S) return NULL; 130 // Return the found array item 131 return F; 132 } 133 134 /// getLongestEntryLength - Return the length of the longest entry in the table. 135 /// 136 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table, 137 size_t Size) { 138 size_t MaxLen = 0; 139 for (size_t i = 0; i < Size; i++) 140 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key)); 141 return MaxLen; 142 } 143 144 /// Display help for feature choices. 145 /// 146 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize, 147 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) { 148 // Determine the length of the longest CPU and Feature entries. 149 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize); 150 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize); 151 152 // Print the CPU table. 153 errs() << "Available CPUs for this target:\n\n"; 154 for (size_t i = 0; i != CPUTableSize; i++) 155 errs() << format(" %-*s - %s.\n", 156 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc); 157 errs() << '\n'; 158 159 // Print the Feature table. 160 errs() << "Available features for this target:\n\n"; 161 for (size_t i = 0; i != FeatTableSize; i++) 162 errs() << format(" %-*s - %s.\n", 163 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc); 164 errs() << '\n'; 165 166 errs() << "Use +feature to enable a feature, or -feature to disable it.\n" 167 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; 168 std::exit(1); 169 } 170 171 //===----------------------------------------------------------------------===// 172 // SubtargetFeatures Implementation 173 //===----------------------------------------------------------------------===// 174 175 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) { 176 // Break up string into separate features 177 Split(Features, Initial); 178 } 179 180 181 std::string SubtargetFeatures::getString() const { 182 return Join(Features); 183 } 184 185 /// SetImpliedBits - For each feature that is (transitively) implied by this 186 /// feature, set it. 187 /// 188 static 189 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry, 190 const SubtargetFeatureKV *FeatureTable, 191 size_t FeatureTableSize) { 192 for (size_t i = 0; i < FeatureTableSize; ++i) { 193 const SubtargetFeatureKV &FE = FeatureTable[i]; 194 195 if (FeatureEntry->Value == FE.Value) continue; 196 197 if (FeatureEntry->Implies & FE.Value) { 198 Bits |= FE.Value; 199 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); 200 } 201 } 202 } 203 204 /// ClearImpliedBits - For each feature that (transitively) implies this 205 /// feature, clear it. 206 /// 207 static 208 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry, 209 const SubtargetFeatureKV *FeatureTable, 210 size_t FeatureTableSize) { 211 for (size_t i = 0; i < FeatureTableSize; ++i) { 212 const SubtargetFeatureKV &FE = FeatureTable[i]; 213 214 if (FeatureEntry->Value == FE.Value) continue; 215 216 if (FE.Implies & FeatureEntry->Value) { 217 Bits &= ~FE.Value; 218 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); 219 } 220 } 221 } 222 223 /// ToggleFeature - Toggle a feature and returns the newly updated feature 224 /// bits. 225 uint64_t 226 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature, 227 const SubtargetFeatureKV *FeatureTable, 228 size_t FeatureTableSize) { 229 // Find feature in table. 230 const SubtargetFeatureKV *FeatureEntry = 231 Find(StripFlag(Feature), FeatureTable, FeatureTableSize); 232 // If there is a match 233 if (FeatureEntry) { 234 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) { 235 Bits &= ~FeatureEntry->Value; 236 237 // For each feature that implies this, clear it. 238 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); 239 } else { 240 Bits |= FeatureEntry->Value; 241 242 // For each feature that this implies, set it. 243 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); 244 } 245 } else { 246 errs() << "'" << Feature 247 << "' is not a recognized feature for this target" 248 << " (ignoring feature)\n"; 249 } 250 251 return Bits; 252 } 253 254 255 /// getFeatureBits - Get feature bits a CPU. 256 /// 257 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU, 258 const SubtargetFeatureKV *CPUTable, 259 size_t CPUTableSize, 260 const SubtargetFeatureKV *FeatureTable, 261 size_t FeatureTableSize) { 262 if (!FeatureTableSize || !CPUTableSize) 263 return 0; 264 265 #ifndef NDEBUG 266 for (size_t i = 1; i < CPUTableSize; i++) { 267 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 && 268 "CPU table is not sorted"); 269 } 270 for (size_t i = 1; i < FeatureTableSize; i++) { 271 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 && 272 "CPU features table is not sorted"); 273 } 274 #endif 275 uint64_t Bits = 0; // Resulting bits 276 277 // Check if help is needed 278 if (CPU == "help") 279 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); 280 281 // Find CPU entry if CPU name is specified. 282 if (!CPU.empty()) { 283 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize); 284 // If there is a match 285 if (CPUEntry) { 286 // Set base feature bits 287 Bits = CPUEntry->Value; 288 289 // Set the feature implied by this CPU feature, if any. 290 for (size_t i = 0; i < FeatureTableSize; ++i) { 291 const SubtargetFeatureKV &FE = FeatureTable[i]; 292 if (CPUEntry->Value & FE.Value) 293 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); 294 } 295 } else { 296 errs() << "'" << CPU 297 << "' is not a recognized processor for this target" 298 << " (ignoring processor)\n"; 299 } 300 } 301 302 // Iterate through each feature 303 for (size_t i = 0, E = Features.size(); i < E; i++) { 304 const StringRef Feature = Features[i]; 305 306 // Check for help 307 if (Feature == "+help") 308 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); 309 310 // Find feature in table. 311 const SubtargetFeatureKV *FeatureEntry = 312 Find(StripFlag(Feature), FeatureTable, FeatureTableSize); 313 // If there is a match 314 if (FeatureEntry) { 315 // Enable/disable feature in bits 316 if (isEnabled(Feature)) { 317 Bits |= FeatureEntry->Value; 318 319 // For each feature that this implies, set it. 320 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); 321 } else { 322 Bits &= ~FeatureEntry->Value; 323 324 // For each feature that implies this, clear it. 325 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); 326 } 327 } else { 328 errs() << "'" << Feature 329 << "' is not a recognized feature for this target" 330 << " (ignoring feature)\n"; 331 } 332 } 333 334 return Bits; 335 } 336 337 /// print - Print feature string. 338 /// 339 void SubtargetFeatures::print(raw_ostream &OS) const { 340 for (size_t i = 0, e = Features.size(); i != e; ++i) 341 OS << Features[i] << " "; 342 OS << "\n"; 343 } 344 345 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 346 /// dump - Dump feature info. 347 /// 348 void SubtargetFeatures::dump() const { 349 print(dbgs()); 350 } 351 #endif 352 353 /// Adds the default features for the specified target triple. 354 /// 355 /// FIXME: This is an inelegant way of specifying the features of a 356 /// subtarget. It would be better if we could encode this information 357 /// into the IR. See <rdar://5972456>. 358 /// 359 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) { 360 if (Triple.getVendor() == Triple::Apple) { 361 if (Triple.getArch() == Triple::ppc) { 362 // powerpc-apple-* 363 AddFeature("altivec"); 364 } else if (Triple.getArch() == Triple::ppc64) { 365 // powerpc64-apple-* 366 AddFeature("64bit"); 367 AddFeature("altivec"); 368 } 369 } 370 } 371