117a519f9SDimitry Andric //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
217a519f9SDimitry Andric //
317a519f9SDimitry Andric //                     The LLVM Compiler Infrastructure
417a519f9SDimitry Andric //
517a519f9SDimitry Andric // This file is distributed under the University of Illinois Open Source
617a519f9SDimitry Andric // License. See LICENSE.TXT for details.
717a519f9SDimitry Andric //
817a519f9SDimitry Andric //===----------------------------------------------------------------------===//
917a519f9SDimitry Andric //
107a7e6055SDimitry Andric /// \file Implements the SubtargetFeature interface.
1117a519f9SDimitry Andric //
1217a519f9SDimitry Andric //===----------------------------------------------------------------------===//
1317a519f9SDimitry Andric 
14db17bf38SDimitry Andric #include "llvm/MC/SubtargetFeature.h"
153ca95b02SDimitry Andric #include "llvm/ADT/ArrayRef.h"
167a7e6055SDimitry Andric #include "llvm/ADT/SmallVector.h"
1797bc6c73SDimitry Andric #include "llvm/ADT/StringExtras.h"
187a7e6055SDimitry Andric #include "llvm/ADT/StringRef.h"
197a7e6055SDimitry Andric #include "llvm/ADT/Triple.h"
20*4ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
217a7e6055SDimitry Andric #include "llvm/Support/Compiler.h"
2217a519f9SDimitry Andric #include "llvm/Support/Debug.h"
23dff0c46cSDimitry Andric #include "llvm/Support/Format.h"
2417a519f9SDimitry Andric #include "llvm/Support/raw_ostream.h"
2517a519f9SDimitry Andric #include <algorithm>
2617a519f9SDimitry Andric #include <cassert>
277a7e6055SDimitry Andric #include <cstddef>
287a7e6055SDimitry Andric #include <cstring>
297a7e6055SDimitry Andric #include <iterator>
307a7e6055SDimitry Andric #include <string>
317a7e6055SDimitry Andric #include <vector>
327a7e6055SDimitry Andric 
3317a519f9SDimitry Andric using namespace llvm;
3417a519f9SDimitry Andric 
357a7e6055SDimitry Andric /// Determine if a feature has a flag; '+' or '-'
hasFlag(StringRef Feature)3639d628a0SDimitry Andric static inline bool hasFlag(StringRef Feature) {
3717a519f9SDimitry Andric   assert(!Feature.empty() && "Empty string");
3817a519f9SDimitry Andric   // Get first character
3917a519f9SDimitry Andric   char Ch = Feature[0];
4017a519f9SDimitry Andric   // Check if first character is '+' or '-' flag
4117a519f9SDimitry Andric   return Ch == '+' || Ch =='-';
4217a519f9SDimitry Andric }
4317a519f9SDimitry Andric 
447a7e6055SDimitry Andric /// Return string stripped of flag.
StripFlag(StringRef Feature)4539d628a0SDimitry Andric static inline std::string StripFlag(StringRef Feature) {
4617a519f9SDimitry Andric   return hasFlag(Feature) ? Feature.substr(1) : Feature;
4717a519f9SDimitry Andric }
4817a519f9SDimitry Andric 
497a7e6055SDimitry Andric /// Return true if enable flag; '+'.
isEnabled(StringRef Feature)5039d628a0SDimitry Andric static inline bool isEnabled(StringRef Feature) {
5117a519f9SDimitry Andric   assert(!Feature.empty() && "Empty string");
5217a519f9SDimitry Andric   // Get first character
5317a519f9SDimitry Andric   char Ch = Feature[0];
5417a519f9SDimitry Andric   // Check if first character is '+' for enabled
5517a519f9SDimitry Andric   return Ch == '+';
5617a519f9SDimitry Andric }
5717a519f9SDimitry Andric 
587a7e6055SDimitry Andric /// Splits a string of comma separated items in to a vector of strings.
Split(std::vector<std::string> & V,StringRef S)5939d628a0SDimitry Andric static void Split(std::vector<std::string> &V, StringRef S) {
6039d628a0SDimitry Andric   SmallVector<StringRef, 3> Tmp;
617d523365SDimitry Andric   S.split(Tmp, ',', -1, false /* KeepEmpty */);
6291bc56edSDimitry Andric   V.assign(Tmp.begin(), Tmp.end());
6317a519f9SDimitry Andric }
6417a519f9SDimitry Andric 
AddFeature(StringRef String,bool Enable)65ff0cc061SDimitry Andric void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
66ff0cc061SDimitry Andric   // Don't add empty features.
6791bc56edSDimitry Andric   if (!String.empty())
6891bc56edSDimitry Andric     // Convert to lowercase, prepend flag if we don't already have a flag.
69ff0cc061SDimitry Andric     Features.push_back(hasFlag(String) ? String.lower()
70ff0cc061SDimitry Andric                                        : (Enable ? "+" : "-") + String.lower());
7117a519f9SDimitry Andric }
7217a519f9SDimitry Andric 
7317a519f9SDimitry Andric /// Find KV in array using binary search.
Find(StringRef S,ArrayRef<SubtargetFeatureKV> A)7491bc56edSDimitry Andric static const SubtargetFeatureKV *Find(StringRef S,
7591bc56edSDimitry Andric                                       ArrayRef<SubtargetFeatureKV> A) {
7617a519f9SDimitry Andric   // Binary search the array
7791bc56edSDimitry Andric   auto F = std::lower_bound(A.begin(), A.end(), S);
7817a519f9SDimitry Andric   // If not found then return NULL
7991bc56edSDimitry Andric   if (F == A.end() || StringRef(F->Key) != S) return nullptr;
8017a519f9SDimitry Andric   // Return the found array item
8117a519f9SDimitry Andric   return F;
8217a519f9SDimitry Andric }
8317a519f9SDimitry Andric 
847a7e6055SDimitry Andric /// Return the length of the longest entry in the table.
getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table)8591bc56edSDimitry Andric static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
8617a519f9SDimitry Andric   size_t MaxLen = 0;
8791bc56edSDimitry Andric   for (auto &I : Table)
8891bc56edSDimitry Andric     MaxLen = std::max(MaxLen, std::strlen(I.Key));
8917a519f9SDimitry Andric   return MaxLen;
9017a519f9SDimitry Andric }
9117a519f9SDimitry Andric 
9217a519f9SDimitry Andric /// Display help for feature choices.
Help(ArrayRef<SubtargetFeatureKV> CPUTable,ArrayRef<SubtargetFeatureKV> FeatTable)9391bc56edSDimitry Andric static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
9491bc56edSDimitry Andric                  ArrayRef<SubtargetFeatureKV> FeatTable) {
9517a519f9SDimitry Andric   // Determine the length of the longest CPU and Feature entries.
9691bc56edSDimitry Andric   unsigned MaxCPULen  = getLongestEntryLength(CPUTable);
9791bc56edSDimitry Andric   unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
9817a519f9SDimitry Andric 
9917a519f9SDimitry Andric   // Print the CPU table.
10017a519f9SDimitry Andric   errs() << "Available CPUs for this target:\n\n";
10191bc56edSDimitry Andric   for (auto &CPU : CPUTable)
10291bc56edSDimitry Andric     errs() << format("  %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
103dff0c46cSDimitry Andric   errs() << '\n';
10417a519f9SDimitry Andric 
10517a519f9SDimitry Andric   // Print the Feature table.
10617a519f9SDimitry Andric   errs() << "Available features for this target:\n\n";
10791bc56edSDimitry Andric   for (auto &Feature : FeatTable)
10891bc56edSDimitry Andric     errs() << format("  %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
109dff0c46cSDimitry Andric   errs() << '\n';
11017a519f9SDimitry Andric 
11117a519f9SDimitry Andric   errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
112dff0c46cSDimitry Andric             "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
11317a519f9SDimitry Andric }
11417a519f9SDimitry Andric 
SubtargetFeatures(StringRef Initial)11539d628a0SDimitry Andric SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
11617a519f9SDimitry Andric   // Break up string into separate features
11717a519f9SDimitry Andric   Split(Features, Initial);
11817a519f9SDimitry Andric }
11917a519f9SDimitry Andric 
getString() const12017a519f9SDimitry Andric std::string SubtargetFeatures::getString() const {
12197bc6c73SDimitry Andric   return join(Features.begin(), Features.end(), ",");
12217a519f9SDimitry Andric }
12317a519f9SDimitry Andric 
1247a7e6055SDimitry Andric /// For each feature that is (transitively) implied by this feature, set it.
12517a519f9SDimitry Andric static
SetImpliedBits(FeatureBitset & Bits,const SubtargetFeatureKV & FeatureEntry,ArrayRef<SubtargetFeatureKV> FeatureTable)1267a7e6055SDimitry Andric void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV &FeatureEntry,
12791bc56edSDimitry Andric                     ArrayRef<SubtargetFeatureKV> FeatureTable) {
1287a7e6055SDimitry Andric   for (const SubtargetFeatureKV &FE : FeatureTable) {
1297a7e6055SDimitry Andric     if (FeatureEntry.Value == FE.Value) continue;
13017a519f9SDimitry Andric 
1317a7e6055SDimitry Andric     if ((FeatureEntry.Implies & FE.Value).any()) {
13217a519f9SDimitry Andric       Bits |= FE.Value;
1337a7e6055SDimitry Andric       SetImpliedBits(Bits, FE, FeatureTable);
13417a519f9SDimitry Andric     }
13517a519f9SDimitry Andric   }
13617a519f9SDimitry Andric }
13717a519f9SDimitry Andric 
1387a7e6055SDimitry Andric /// For each feature that (transitively) implies this feature, clear it.
13917a519f9SDimitry Andric static
ClearImpliedBits(FeatureBitset & Bits,const SubtargetFeatureKV & FeatureEntry,ArrayRef<SubtargetFeatureKV> FeatureTable)140ff0cc061SDimitry Andric void ClearImpliedBits(FeatureBitset &Bits,
1417a7e6055SDimitry Andric                       const SubtargetFeatureKV &FeatureEntry,
14291bc56edSDimitry Andric                       ArrayRef<SubtargetFeatureKV> FeatureTable) {
1437a7e6055SDimitry Andric   for (const SubtargetFeatureKV &FE : FeatureTable) {
1447a7e6055SDimitry Andric     if (FeatureEntry.Value == FE.Value) continue;
14517a519f9SDimitry Andric 
1467a7e6055SDimitry Andric     if ((FE.Implies & FeatureEntry.Value).any()) {
14717a519f9SDimitry Andric       Bits &= ~FE.Value;
1487a7e6055SDimitry Andric       ClearImpliedBits(Bits, FE, FeatureTable);
14917a519f9SDimitry Andric     }
15017a519f9SDimitry Andric   }
15117a519f9SDimitry Andric }
15217a519f9SDimitry Andric 
1534d0b32cdSDimitry Andric void
ToggleFeature(FeatureBitset & Bits,StringRef Feature,ArrayRef<SubtargetFeatureKV> FeatureTable)1544d0b32cdSDimitry Andric SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
15591bc56edSDimitry Andric                                  ArrayRef<SubtargetFeatureKV> FeatureTable) {
15617a519f9SDimitry Andric   // Find feature in table.
15717a519f9SDimitry Andric   const SubtargetFeatureKV *FeatureEntry =
15891bc56edSDimitry Andric       Find(StripFlag(Feature), FeatureTable);
15917a519f9SDimitry Andric   // If there is a match
16017a519f9SDimitry Andric   if (FeatureEntry) {
16117a519f9SDimitry Andric     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
16217a519f9SDimitry Andric       Bits &= ~FeatureEntry->Value;
16317a519f9SDimitry Andric       // For each feature that implies this, clear it.
1647a7e6055SDimitry Andric       ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
16517a519f9SDimitry Andric     } else {
16617a519f9SDimitry Andric       Bits |=  FeatureEntry->Value;
16717a519f9SDimitry Andric 
16817a519f9SDimitry Andric       // For each feature that this implies, set it.
1697a7e6055SDimitry Andric       SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
17017a519f9SDimitry Andric     }
17117a519f9SDimitry Andric   } else {
1727a7e6055SDimitry Andric     errs() << "'" << Feature << "' is not a recognized feature for this target"
17317a519f9SDimitry Andric            << " (ignoring feature)\n";
17417a519f9SDimitry Andric   }
17517a519f9SDimitry Andric }
17617a519f9SDimitry Andric 
ApplyFeatureFlag(FeatureBitset & Bits,StringRef Feature,ArrayRef<SubtargetFeatureKV> FeatureTable)1774d0b32cdSDimitry Andric void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
17897bc6c73SDimitry Andric                                     ArrayRef<SubtargetFeatureKV> FeatureTable) {
17997bc6c73SDimitry Andric   assert(hasFlag(Feature));
18097bc6c73SDimitry Andric 
18197bc6c73SDimitry Andric   // Find feature in table.
18297bc6c73SDimitry Andric   const SubtargetFeatureKV *FeatureEntry =
18397bc6c73SDimitry Andric       Find(StripFlag(Feature), FeatureTable);
18497bc6c73SDimitry Andric   // If there is a match
18597bc6c73SDimitry Andric   if (FeatureEntry) {
18697bc6c73SDimitry Andric     // Enable/disable feature in bits
18797bc6c73SDimitry Andric     if (isEnabled(Feature)) {
18897bc6c73SDimitry Andric       Bits |= FeatureEntry->Value;
18997bc6c73SDimitry Andric 
19097bc6c73SDimitry Andric       // For each feature that this implies, set it.
1917a7e6055SDimitry Andric       SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
19297bc6c73SDimitry Andric     } else {
19397bc6c73SDimitry Andric       Bits &= ~FeatureEntry->Value;
19497bc6c73SDimitry Andric 
19597bc6c73SDimitry Andric       // For each feature that implies this, clear it.
1967a7e6055SDimitry Andric       ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
19797bc6c73SDimitry Andric     }
19897bc6c73SDimitry Andric   } else {
1997a7e6055SDimitry Andric     errs() << "'" << Feature << "' is not a recognized feature for this target"
20097bc6c73SDimitry Andric            << " (ignoring feature)\n";
20197bc6c73SDimitry Andric   }
20297bc6c73SDimitry Andric }
20397bc6c73SDimitry Andric 
204ff0cc061SDimitry Andric FeatureBitset
getFeatureBits(StringRef CPU,ArrayRef<SubtargetFeatureKV> CPUTable,ArrayRef<SubtargetFeatureKV> FeatureTable)20539d628a0SDimitry Andric SubtargetFeatures::getFeatureBits(StringRef CPU,
20691bc56edSDimitry Andric                                   ArrayRef<SubtargetFeatureKV> CPUTable,
20791bc56edSDimitry Andric                                   ArrayRef<SubtargetFeatureKV> FeatureTable) {
20891bc56edSDimitry Andric   if (CPUTable.empty() || FeatureTable.empty())
209ff0cc061SDimitry Andric     return FeatureBitset();
21017a519f9SDimitry Andric 
2114d0b32cdSDimitry Andric   assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
21217a519f9SDimitry Andric          "CPU table is not sorted");
2134d0b32cdSDimitry Andric   assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
21417a519f9SDimitry Andric          "CPU features table is not sorted");
215ff0cc061SDimitry Andric   // Resulting bits
216ff0cc061SDimitry Andric   FeatureBitset Bits;
21717a519f9SDimitry Andric 
21817a519f9SDimitry Andric   // Check if help is needed
21917a519f9SDimitry Andric   if (CPU == "help")
22091bc56edSDimitry Andric     Help(CPUTable, FeatureTable);
22117a519f9SDimitry Andric 
22217a519f9SDimitry Andric   // Find CPU entry if CPU name is specified.
22391bc56edSDimitry Andric   else if (!CPU.empty()) {
22491bc56edSDimitry Andric     const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
22591bc56edSDimitry Andric 
22617a519f9SDimitry Andric     // If there is a match
22717a519f9SDimitry Andric     if (CPUEntry) {
22817a519f9SDimitry Andric       // Set base feature bits
22917a519f9SDimitry Andric       Bits = CPUEntry->Value;
23017a519f9SDimitry Andric 
23117a519f9SDimitry Andric       // Set the feature implied by this CPU feature, if any.
23291bc56edSDimitry Andric       for (auto &FE : FeatureTable) {
233ff0cc061SDimitry Andric         if ((CPUEntry->Value & FE.Value).any())
2347a7e6055SDimitry Andric           SetImpliedBits(Bits, FE, FeatureTable);
23517a519f9SDimitry Andric       }
23617a519f9SDimitry Andric     } else {
2377a7e6055SDimitry Andric       errs() << "'" << CPU << "' is not a recognized processor for this target"
23817a519f9SDimitry Andric              << " (ignoring processor)\n";
23917a519f9SDimitry Andric     }
24017a519f9SDimitry Andric   }
24117a519f9SDimitry Andric 
24217a519f9SDimitry Andric   // Iterate through each feature
2437a7e6055SDimitry Andric   for (const std::string &Feature : Features) {
24417a519f9SDimitry Andric     // Check for help
24517a519f9SDimitry Andric     if (Feature == "+help")
24691bc56edSDimitry Andric       Help(CPUTable, FeatureTable);
24717a519f9SDimitry Andric 
2484d0b32cdSDimitry Andric     ApplyFeatureFlag(Bits, Feature, FeatureTable);
24917a519f9SDimitry Andric   }
25017a519f9SDimitry Andric 
25117a519f9SDimitry Andric   return Bits;
25217a519f9SDimitry Andric }
25317a519f9SDimitry Andric 
print(raw_ostream & OS) const25417a519f9SDimitry Andric void SubtargetFeatures::print(raw_ostream &OS) const {
25591bc56edSDimitry Andric   for (auto &F : Features)
25691bc56edSDimitry Andric     OS << F << " ";
25717a519f9SDimitry Andric   OS << "\n";
25817a519f9SDimitry Andric }
25917a519f9SDimitry Andric 
2607a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const2613ca95b02SDimitry Andric LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
26217a519f9SDimitry Andric   print(dbgs());
26317a519f9SDimitry Andric }
2647a7e6055SDimitry Andric #endif
26517a519f9SDimitry Andric 
getDefaultSubtargetFeatures(const Triple & Triple)26617a519f9SDimitry Andric void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
2677a7e6055SDimitry Andric   // FIXME: This is an inelegant way of specifying the features of a
2687a7e6055SDimitry Andric   // subtarget. It would be better if we could encode this information
2697a7e6055SDimitry Andric   // into the IR. See <rdar://5972456>.
27017a519f9SDimitry Andric   if (Triple.getVendor() == Triple::Apple) {
27117a519f9SDimitry Andric     if (Triple.getArch() == Triple::ppc) {
27217a519f9SDimitry Andric       // powerpc-apple-*
27317a519f9SDimitry Andric       AddFeature("altivec");
27417a519f9SDimitry Andric     } else if (Triple.getArch() == Triple::ppc64) {
27517a519f9SDimitry Andric       // powerpc64-apple-*
27617a519f9SDimitry Andric       AddFeature("64bit");
27717a519f9SDimitry Andric       AddFeature("altivec");
27817a519f9SDimitry Andric     }
27917a519f9SDimitry Andric   }
28017a519f9SDimitry Andric }
281