Home | History | Annotate | Download | only in MC
      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/raw_ostream.h"
     17 #include "llvm/ADT/StringExtras.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(LowercaseString(String), IsEnabled));
    118   }
    119 }
    120 
    121 /// Find KV in array using binary search.
    122 template<typename T> const T *Find(const StringRef S, const T *A, size_t L) {
    123   // Make the lower bound element we're looking for
    124   T KV;
    125   KV.Key = S.data();
    126   // Determine the end of the array
    127   const T *Hi = A + L;
    128   // Binary search the array
    129   const T *F = std::lower_bound(A, Hi, KV);
    130   // If not found then return NULL
    131   if (F == Hi || StringRef(F->Key) != S) return NULL;
    132   // Return the found array item
    133   return F;
    134 }
    135 
    136 /// getLongestEntryLength - Return the length of the longest entry in the table.
    137 ///
    138 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
    139                                     size_t Size) {
    140   size_t MaxLen = 0;
    141   for (size_t i = 0; i < Size; i++)
    142     MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
    143   return MaxLen;
    144 }
    145 
    146 /// Display help for feature choices.
    147 ///
    148 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
    149                  const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
    150   // Determine the length of the longest CPU and Feature entries.
    151   unsigned MaxCPULen  = getLongestEntryLength(CPUTable, CPUTableSize);
    152   unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
    153 
    154   // Print the CPU table.
    155   errs() << "Available CPUs for this target:\n\n";
    156   for (size_t i = 0; i != CPUTableSize; i++)
    157     errs() << "  " << CPUTable[i].Key
    158          << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
    159          << " - " << CPUTable[i].Desc << ".\n";
    160   errs() << "\n";
    161 
    162   // Print the Feature table.
    163   errs() << "Available features for this target:\n\n";
    164   for (size_t i = 0; i != FeatTableSize; i++)
    165     errs() << "  " << FeatTable[i].Key
    166          << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
    167          << " - " << FeatTable[i].Desc << ".\n";
    168   errs() << "\n";
    169 
    170   errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
    171        << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
    172   std::exit(1);
    173 }
    174 
    175 //===----------------------------------------------------------------------===//
    176 //                    SubtargetFeatures Implementation
    177 //===----------------------------------------------------------------------===//
    178 
    179 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
    180   // Break up string into separate features
    181   Split(Features, Initial);
    182 }
    183 
    184 
    185 std::string SubtargetFeatures::getString() const {
    186   return Join(Features);
    187 }
    188 
    189 /// SetImpliedBits - For each feature that is (transitively) implied by this
    190 /// feature, set it.
    191 ///
    192 static
    193 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
    194                     const SubtargetFeatureKV *FeatureTable,
    195                     size_t FeatureTableSize) {
    196   for (size_t i = 0; i < FeatureTableSize; ++i) {
    197     const SubtargetFeatureKV &FE = FeatureTable[i];
    198 
    199     if (FeatureEntry->Value == FE.Value) continue;
    200 
    201     if (FeatureEntry->Implies & FE.Value) {
    202       Bits |= FE.Value;
    203       SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
    204     }
    205   }
    206 }
    207 
    208 /// ClearImpliedBits - For each feature that (transitively) implies this
    209 /// feature, clear it.
    210 ///
    211 static
    212 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
    213                       const SubtargetFeatureKV *FeatureTable,
    214                       size_t FeatureTableSize) {
    215   for (size_t i = 0; i < FeatureTableSize; ++i) {
    216     const SubtargetFeatureKV &FE = FeatureTable[i];
    217 
    218     if (FeatureEntry->Value == FE.Value) continue;
    219 
    220     if (FE.Implies & FeatureEntry->Value) {
    221       Bits &= ~FE.Value;
    222       ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
    223     }
    224   }
    225 }
    226 
    227 /// ToggleFeature - Toggle a feature and returns the newly updated feature
    228 /// bits.
    229 uint64_t
    230 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
    231                                  const SubtargetFeatureKV *FeatureTable,
    232                                  size_t FeatureTableSize) {
    233   // Find feature in table.
    234   const SubtargetFeatureKV *FeatureEntry =
    235     Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
    236   // If there is a match
    237   if (FeatureEntry) {
    238     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
    239       Bits &= ~FeatureEntry->Value;
    240 
    241       // For each feature that implies this, clear it.
    242       ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
    243     } else {
    244       Bits |=  FeatureEntry->Value;
    245 
    246       // For each feature that this implies, set it.
    247       SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
    248     }
    249   } else {
    250     errs() << "'" << Feature
    251            << "' is not a recognized feature for this target"
    252            << " (ignoring feature)\n";
    253   }
    254 
    255   return Bits;
    256 }
    257 
    258 
    259 /// getFeatureBits - Get feature bits a CPU.
    260 ///
    261 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
    262                                          const SubtargetFeatureKV *CPUTable,
    263                                          size_t CPUTableSize,
    264                                          const SubtargetFeatureKV *FeatureTable,
    265                                          size_t FeatureTableSize) {
    266   if (!FeatureTableSize || !CPUTableSize)
    267     return 0;
    268 
    269 #ifndef NDEBUG
    270   for (size_t i = 1; i < CPUTableSize; i++) {
    271     assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
    272            "CPU table is not sorted");
    273   }
    274   for (size_t i = 1; i < FeatureTableSize; i++) {
    275     assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
    276           "CPU features table is not sorted");
    277   }
    278 #endif
    279   uint64_t Bits = 0;                    // Resulting bits
    280 
    281   // Check if help is needed
    282   if (CPU == "help")
    283     Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
    284 
    285   // Find CPU entry if CPU name is specified.
    286   if (!CPU.empty()) {
    287     const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
    288     // If there is a match
    289     if (CPUEntry) {
    290       // Set base feature bits
    291       Bits = CPUEntry->Value;
    292 
    293       // Set the feature implied by this CPU feature, if any.
    294       for (size_t i = 0; i < FeatureTableSize; ++i) {
    295         const SubtargetFeatureKV &FE = FeatureTable[i];
    296         if (CPUEntry->Value & FE.Value)
    297           SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
    298       }
    299     } else {
    300       errs() << "'" << CPU
    301              << "' is not a recognized processor for this target"
    302              << " (ignoring processor)\n";
    303     }
    304   }
    305 
    306   // Iterate through each feature
    307   for (size_t i = 0, E = Features.size(); i < E; i++) {
    308     const StringRef Feature = Features[i];
    309 
    310     // Check for help
    311     if (Feature == "+help")
    312       Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
    313 
    314     // Find feature in table.
    315     const SubtargetFeatureKV *FeatureEntry =
    316                        Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
    317     // If there is a match
    318     if (FeatureEntry) {
    319       // Enable/disable feature in bits
    320       if (isEnabled(Feature)) {
    321         Bits |=  FeatureEntry->Value;
    322 
    323         // For each feature that this implies, set it.
    324         SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
    325       } else {
    326         Bits &= ~FeatureEntry->Value;
    327 
    328         // For each feature that implies this, clear it.
    329         ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
    330       }
    331     } else {
    332       errs() << "'" << Feature
    333              << "' is not a recognized feature for this target"
    334              << " (ignoring feature)\n";
    335     }
    336   }
    337 
    338   return Bits;
    339 }
    340 
    341 /// Get scheduling itinerary of a CPU.
    342 void *SubtargetFeatures::getItinerary(const StringRef CPU,
    343                                       const SubtargetInfoKV *Table,
    344                                       size_t TableSize) {
    345   assert(Table && "missing table");
    346 #ifndef NDEBUG
    347   for (size_t i = 1; i < TableSize; i++) {
    348     assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
    349   }
    350 #endif
    351 
    352   // Find entry
    353   const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize);
    354 
    355   if (Entry) {
    356     return Entry->Value;
    357   } else {
    358     errs() << "'" << CPU
    359            << "' is not a recognized processor for this target"
    360            << " (ignoring processor)\n";
    361     return NULL;
    362   }
    363 }
    364 
    365 /// print - Print feature string.
    366 ///
    367 void SubtargetFeatures::print(raw_ostream &OS) const {
    368   for (size_t i = 0, e = Features.size(); i != e; ++i)
    369     OS << Features[i] << "  ";
    370   OS << "\n";
    371 }
    372 
    373 /// dump - Dump feature info.
    374 ///
    375 void SubtargetFeatures::dump() const {
    376   print(dbgs());
    377 }
    378 
    379 /// getDefaultSubtargetFeatures - Return a string listing the features
    380 /// associated with the target triple.
    381 ///
    382 /// FIXME: This is an inelegant way of specifying the features of a
    383 /// subtarget. It would be better if we could encode this information
    384 /// into the IR. See <rdar://5972456>.
    385 ///
    386 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
    387   if (Triple.getVendor() == Triple::Apple) {
    388     if (Triple.getArch() == Triple::ppc) {
    389       // powerpc-apple-*
    390       AddFeature("altivec");
    391     } else if (Triple.getArch() == Triple::ppc64) {
    392       // powerpc64-apple-*
    393       AddFeature("64bit");
    394       AddFeature("altivec");
    395     }
    396   }
    397 }
    398