LCOV - code coverage report
Current view: top level - lib/MC - SubtargetFeature.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 80 110 72.7 %
Date: 2017-09-14 15:23:50 Functions: 12 15 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : /// \file Implements the SubtargetFeature interface.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/MC/SubtargetFeature.h"
      15             : #include "llvm/ADT/ArrayRef.h"
      16             : #include "llvm/ADT/SmallVector.h"
      17             : #include "llvm/ADT/StringExtras.h"
      18             : #include "llvm/ADT/StringRef.h"
      19             : #include "llvm/ADT/Triple.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      357346 :   char Ch = Feature[0];
      39             :   // Check if first character is '+' or '-' flag
      40      178673 :   return Ch == '+' || Ch =='-';
      41             : }
      42             : 
      43             : /// Return string stripped of flag.
      44      172041 : static inline std::string StripFlag(StringRef Feature) {
      45      687904 :   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      343150 :   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      113259 : static void Split(std::vector<std::string> &V, StringRef S) {
      59      226518 :   SmallVector<StringRef, 3> Tmp;
      60      113259 :   S.split(Tmp, ',', -1, false /* KeepEmpty */);
      61      339779 :   V.assign(Tmp.begin(), Tmp.end());
      62      113259 : }
      63             : 
      64        6633 : void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
      65             :   // Don't add empty features.
      66        6633 :   if (!String.empty())
      67             :     // Convert to lowercase, prepend flag if we don't already have a flag.
      68       27365 :     Features.push_back(hasFlag(String) ? String.lower()
      69       14101 :                                        : (Enable ? "+" : "-") + String.lower());
      70        6633 : }
      71             : 
      72             : /// Find KV in array using binary search.
      73      240078 : static const SubtargetFeatureKV *Find(StringRef S,
      74             :                                       ArrayRef<SubtargetFeatureKV> A) {
      75             :   // Binary search the array
      76      480157 :   auto F = std::lower_bound(A.begin(), A.end(), S);
      77             :   // If not found then return NULL
      78      480158 :   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           0 : static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
      85           0 :   size_t MaxLen = 0;
      86           0 :   for (auto &I : Table)
      87           0 :     MaxLen = std::max(MaxLen, std::strlen(I.Key));
      88           0 :   return MaxLen;
      89             : }
      90             : 
      91             : /// Display help for feature choices.
      92           0 : static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
      93             :                  ArrayRef<SubtargetFeatureKV> FeatTable) {
      94             :   // Determine the length of the longest CPU and Feature entries.
      95           0 :   unsigned MaxCPULen  = getLongestEntryLength(CPUTable);
      96           0 :   unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
      97             : 
      98             :   // Print the CPU table.
      99           0 :   errs() << "Available CPUs for this target:\n\n";
     100           0 :   for (auto &CPU : CPUTable)
     101           0 :     errs() << format("  %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
     102           0 :   errs() << '\n';
     103             : 
     104             :   // Print the Feature table.
     105           0 :   errs() << "Available features for this target:\n\n";
     106           0 :   for (auto &Feature : FeatTable)
     107           0 :     errs() << format("  %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
     108           0 :   errs() << '\n';
     109             : 
     110           0 :   errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
     111             :             "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
     112           0 : }
     113             : 
     114      226520 : SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
     115             :   // Break up string into separate features
     116      113260 :   Split(Features, Initial);
     117      113259 : }
     118             : 
     119       21195 : std::string SubtargetFeatures::getString() const {
     120       63585 :   return join(Features.begin(), Features.end(), ",");
     121             : }
     122             : 
     123             : /// For each feature that is (transitively) implied by this feature, set it.
     124             : static
     125     1261096 : void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV &FeatureEntry,
     126             :                     ArrayRef<SubtargetFeatureKV> FeatureTable) {
     127   110679819 :   for (const SubtargetFeatureKV &FE : FeatureTable) {
     128   108157717 :     if (FeatureEntry.Value == FE.Value) continue;
     129             : 
     130   213793420 :     if ((FeatureEntry.Implies & FE.Value).any()) {
     131      855222 :       Bits |= FE.Value;
     132      855222 :       SetImpliedBits(Bits, FE, FeatureTable);
     133             :     }
     134             :   }
     135     1261006 : }
     136             : 
     137             : /// For each feature that (transitively) implies this feature, clear it.
     138             : static
     139       75988 : void ClearImpliedBits(FeatureBitset &Bits,
     140             :                       const SubtargetFeatureKV &FeatureEntry,
     141             :                       ArrayRef<SubtargetFeatureKV> FeatureTable) {
     142     6949043 :   for (const SubtargetFeatureKV &FE : FeatureTable) {
     143     6797013 :     if (FeatureEntry.Value == FE.Value) continue;
     144             : 
     145    13441942 :     if ((FE.Implies & FeatureEntry.Value).any()) {
     146       94560 :       Bits &= ~FE.Value;
     147       31520 :       ClearImpliedBits(Bits, FE, FeatureTable);
     148             :     }
     149             :   }
     150       76042 : }
     151             : 
     152             : void
     153         259 : SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
     154             :                                  ArrayRef<SubtargetFeatureKV> FeatureTable) {
     155             :   // Find feature in table.
     156             :   const SubtargetFeatureKV *FeatureEntry =
     157         777 :       Find(StripFlag(Feature), FeatureTable);
     158             :   // If there is a match
     159         259 :   if (FeatureEntry) {
     160         518 :     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
     161         120 :       Bits &= ~FeatureEntry->Value;
     162             :       // For each feature that implies this, clear it.
     163          40 :       ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
     164             :     } else {
     165         219 :       Bits |=  FeatureEntry->Value;
     166             : 
     167             :       // For each feature that this implies, set it.
     168         219 :       SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
     169             :     }
     170             :   } else {
     171           0 :     errs() << "'" << Feature << "' is not a recognized feature for this target"
     172           0 :            << " (ignoring feature)\n";
     173             :   }
     174         259 : }
     175             : 
     176      171782 : void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
     177             :                                     ArrayRef<SubtargetFeatureKV> FeatureTable) {
     178             :   assert(hasFlag(Feature));
     179             : 
     180             :   // Find feature in table.
     181             :   const SubtargetFeatureKV *FeatureEntry =
     182      515345 :       Find(StripFlag(Feature), FeatureTable);
     183             :   // If there is a match
     184      171782 :   if (FeatureEntry) {
     185             :     // Enable/disable feature in bits
     186      343150 :     if (isEnabled(Feature)) {
     187      127093 :       Bits |= FeatureEntry->Value;
     188             : 
     189             :       // For each feature that this implies, set it.
     190      127093 :       SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
     191             :     } else {
     192      133446 :       Bits &= ~FeatureEntry->Value;
     193             : 
     194             :       // For each feature that implies this, clear it.
     195       44482 :       ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
     196             :     }
     197             :   } else {
     198         207 :     errs() << "'" << Feature << "' is not a recognized feature for this target"
     199         207 :            << " (ignoring feature)\n";
     200             :   }
     201      171782 : }
     202             : 
     203             : FeatureBitset
     204       92065 : SubtargetFeatures::getFeatureBits(StringRef CPU,
     205             :                                   ArrayRef<SubtargetFeatureKV> CPUTable,
     206             :                                   ArrayRef<SubtargetFeatureKV> FeatureTable) {
     207       92065 :   if (CPUTable.empty() || FeatureTable.empty())
     208         654 :     return FeatureBitset();
     209             : 
     210             :   assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
     211             :          "CPU table is not sorted");
     212             :   assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
     213             :          "CPU features table is not sorted");
     214             :   // Resulting bits
     215       91847 :   FeatureBitset Bits;
     216             : 
     217             :   // Check if help is needed
     218       91847 :   if (CPU == "help")
     219           0 :     Help(CPUTable, FeatureTable);
     220             : 
     221             :   // Find CPU entry if CPU name is specified.
     222       91847 :   else if (!CPU.empty()) {
     223       68038 :     const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
     224             : 
     225             :     // If there is a match
     226       68038 :     if (CPUEntry) {
     227             :       // Set base feature bits
     228       67979 :       Bits = CPUEntry->Value;
     229             : 
     230             :       // Set the feature implied by this CPU feature, if any.
     231     5309011 :       for (auto &FE : FeatureTable) {
     232    10346106 :         if ((CPUEntry->Value & FE.Value).any())
     233      278473 :           SetImpliedBits(Bits, FE, FeatureTable);
     234             :       }
     235             :     } else {
     236          59 :       errs() << "'" << CPU << "' is not a recognized processor for this target"
     237          59 :              << " (ignoring processor)\n";
     238             :     }
     239             :   }
     240             : 
     241             :   // Iterate through each feature
     242      538760 :   for (const std::string &Feature : Features) {
     243             :     // Check for help
     244      171372 :     if (Feature == "+help")
     245           0 :       Help(CPUTable, FeatureTable);
     246             : 
     247      171372 :     ApplyFeatureFlag(Bits, Feature, FeatureTable);
     248             :   }
     249             : 
     250       91847 :   return Bits;
     251             : }
     252             : 
     253           0 : void SubtargetFeatures::print(raw_ostream &OS) const {
     254           0 :   for (auto &F : Features)
     255           0 :     OS << F << " ";
     256           0 :   OS << "\n";
     257           0 : }
     258             : 
     259             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     260             : LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
     261             :   print(dbgs());
     262             : }
     263             : #endif
     264             : 
     265         518 : void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
     266             :   // FIXME: This is an inelegant way of specifying the features of a
     267             :   // subtarget. It would be better if we could encode this information
     268             :   // into the IR. See <rdar://5972456>.
     269         518 :   if (Triple.getVendor() == Triple::Apple) {
     270          87 :     if (Triple.getArch() == Triple::ppc) {
     271             :       // powerpc-apple-*
     272           0 :       AddFeature("altivec");
     273          87 :     } else if (Triple.getArch() == Triple::ppc64) {
     274             :       // powerpc64-apple-*
     275           0 :       AddFeature("64bit");
     276           0 :       AddFeature("altivec");
     277             :     }
     278             :   }
     279         518 : }

Generated by: LCOV version 1.13