2005-09-02 05:36:18 +08:00
|
|
|
//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-09-02 05:36:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-02-21 09:27:29 +08:00
|
|
|
/// \file Implements the SubtargetFeature interface.
|
2005-09-02 05:36:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2016-04-18 17:17:29 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2015-05-28 19:45:32 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-01-05 09:29:36 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-10-17 00:30:34 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2009-08-24 05:41:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2005-09-02 05:36:18 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2017-02-09 09:09:54 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2005-09-02 05:36:18 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
/// Determine if a feature has a flag; '+' or '-'
|
2014-08-31 00:48:02 +08:00
|
|
|
static inline bool hasFlag(StringRef Feature) {
|
2005-10-23 13:26:26 +08:00
|
|
|
assert(!Feature.empty() && "Empty string");
|
|
|
|
// Get first character
|
|
|
|
char Ch = Feature[0];
|
|
|
|
// Check if first character is '+' or '-' flag
|
|
|
|
return Ch == '+' || Ch =='-';
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
/// Return string stripped of flag.
|
2014-08-31 00:48:02 +08:00
|
|
|
static inline std::string StripFlag(StringRef Feature) {
|
2005-10-23 13:26:26 +08:00
|
|
|
return hasFlag(Feature) ? Feature.substr(1) : Feature;
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
/// Return true if enable flag; '+'.
|
2014-08-31 00:48:02 +08:00
|
|
|
static inline bool isEnabled(StringRef Feature) {
|
2005-10-23 13:26:26 +08:00
|
|
|
assert(!Feature.empty() && "Empty string");
|
|
|
|
// Get first character
|
|
|
|
char Ch = Feature[0];
|
|
|
|
// Check if first character is '+' for enabled
|
|
|
|
return Ch == '+';
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
/// Splits a string of comma separated items in to a vector of strings.
|
2014-08-31 00:48:02 +08:00
|
|
|
static void Split(std::vector<std::string> &V, StringRef S) {
|
2014-08-11 10:21:32 +08:00
|
|
|
SmallVector<StringRef, 3> Tmp;
|
2015-09-10 14:12:31 +08:00
|
|
|
S.split(Tmp, ',', -1, false /* KeepEmpty */);
|
2014-05-14 03:55:17 +08:00
|
|
|
V.assign(Tmp.begin(), Tmp.end());
|
2005-09-02 05:36:18 +08:00
|
|
|
}
|
|
|
|
|
2015-03-31 13:52:57 +08:00
|
|
|
void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
|
2015-03-28 11:24:19 +08:00
|
|
|
// Don't add empty features.
|
2014-05-06 10:37:26 +08:00
|
|
|
if (!String.empty())
|
|
|
|
// Convert to lowercase, prepend flag if we don't already have a flag.
|
2015-03-31 13:52:57 +08:00
|
|
|
Features.push_back(hasFlag(String) ? String.lower()
|
|
|
|
: (Enable ? "+" : "-") + String.lower());
|
2005-09-02 05:36:18 +08:00
|
|
|
}
|
|
|
|
|
2005-10-25 23:15:28 +08:00
|
|
|
/// Find KV in array using binary search.
|
2014-05-07 04:23:04 +08:00
|
|
|
static const SubtargetFeatureKV *Find(StringRef S,
|
|
|
|
ArrayRef<SubtargetFeatureKV> A) {
|
2005-09-02 05:36:18 +08:00
|
|
|
// Binary search the array
|
2014-05-07 04:23:04 +08:00
|
|
|
auto F = std::lower_bound(A.begin(), A.end(), S);
|
2005-09-02 05:36:18 +08:00
|
|
|
// If not found then return NULL
|
2014-05-07 04:23:04 +08:00
|
|
|
if (F == A.end() || StringRef(F->Key) != S) return nullptr;
|
2005-09-02 05:36:18 +08:00
|
|
|
// Return the found array item
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
/// Return the length of the longest entry in the table.
|
2014-05-07 04:23:04 +08:00
|
|
|
static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
|
2005-10-23 13:26:26 +08:00
|
|
|
size_t MaxLen = 0;
|
2014-05-07 04:23:04 +08:00
|
|
|
for (auto &I : Table)
|
|
|
|
MaxLen = std::max(MaxLen, std::strlen(I.Key));
|
2005-10-24 06:23:13 +08:00
|
|
|
return MaxLen;
|
|
|
|
}
|
2005-10-23 13:33:39 +08:00
|
|
|
|
2005-10-24 06:23:13 +08:00
|
|
|
/// Display help for feature choices.
|
2014-05-07 04:23:04 +08:00
|
|
|
static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
|
|
|
|
ArrayRef<SubtargetFeatureKV> FeatTable) {
|
2005-10-24 06:23:13 +08:00
|
|
|
// Determine the length of the longest CPU and Feature entries.
|
2014-05-07 04:23:04 +08:00
|
|
|
unsigned MaxCPULen = getLongestEntryLength(CPUTable);
|
|
|
|
unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
|
2005-10-23 13:33:39 +08:00
|
|
|
|
2005-10-24 06:23:13 +08:00
|
|
|
// Print the CPU table.
|
2009-08-24 05:41:43 +08:00
|
|
|
errs() << "Available CPUs for this target:\n\n";
|
2014-05-07 04:23:04 +08:00
|
|
|
for (auto &CPU : CPUTable)
|
|
|
|
errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
|
2011-10-17 00:30:34 +08:00
|
|
|
errs() << '\n';
|
|
|
|
|
2005-10-24 06:23:13 +08:00
|
|
|
// Print the Feature table.
|
2009-08-24 05:41:43 +08:00
|
|
|
errs() << "Available features for this target:\n\n";
|
2014-05-07 04:23:04 +08:00
|
|
|
for (auto &Feature : FeatTable)
|
|
|
|
errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
|
2011-10-17 00:30:34 +08:00
|
|
|
errs() << '\n';
|
|
|
|
|
2009-08-24 05:41:43 +08:00
|
|
|
errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
|
2011-10-17 00:30:34 +08:00
|
|
|
"For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
|
2005-10-23 13:26:26 +08:00
|
|
|
}
|
|
|
|
|
2014-08-31 00:48:02 +08:00
|
|
|
SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
|
2005-10-23 13:26:26 +08:00
|
|
|
// Break up string into separate features
|
|
|
|
Split(Features, Initial);
|
2005-09-03 03:27:43 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 12:40:50 +08:00
|
|
|
std::string SubtargetFeatures::getString() const {
|
2015-05-28 19:45:32 +08:00
|
|
|
return join(Features.begin(), Features.end(), ",");
|
2005-10-23 13:26:26 +08:00
|
|
|
}
|
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
/// For each feature that is (transitively) implied by this feature, set it.
|
2007-05-05 04:38:40 +08:00
|
|
|
static
|
2017-02-21 09:27:29 +08:00
|
|
|
void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV &FeatureEntry,
|
2014-05-07 04:23:04 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
2017-02-21 09:27:29 +08:00
|
|
|
for (const SubtargetFeatureKV &FE : FeatureTable) {
|
|
|
|
if (FeatureEntry.Value == FE.Value) continue;
|
2007-05-05 04:38:40 +08:00
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
if ((FeatureEntry.Implies & FE.Value).any()) {
|
2007-05-05 04:38:40 +08:00
|
|
|
Bits |= FE.Value;
|
2017-02-21 09:27:29 +08:00
|
|
|
SetImpliedBits(Bits, FE, FeatureTable);
|
2007-05-05 04:38:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
/// For each feature that (transitively) implies this feature, clear it.
|
2007-05-05 04:38:40 +08:00
|
|
|
static
|
2017-02-21 09:27:29 +08:00
|
|
|
void ClearImpliedBits(FeatureBitset &Bits,
|
|
|
|
const SubtargetFeatureKV &FeatureEntry,
|
2014-05-07 04:23:04 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
2017-02-21 09:27:29 +08:00
|
|
|
for (const SubtargetFeatureKV &FE : FeatureTable) {
|
|
|
|
if (FeatureEntry.Value == FE.Value) continue;
|
2007-05-05 04:38:40 +08:00
|
|
|
|
2017-02-21 09:27:29 +08:00
|
|
|
if ((FE.Implies & FeatureEntry.Value).any()) {
|
2007-05-05 04:38:40 +08:00
|
|
|
Bits &= ~FE.Value;
|
2017-02-21 09:27:29 +08:00
|
|
|
ClearImpliedBits(Bits, FE, FeatureTable);
|
2007-05-05 04:38:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-25 23:15:28 +08:00
|
|
|
|
2016-01-05 18:25:56 +08:00
|
|
|
void
|
|
|
|
SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
|
2014-05-07 05:04:27 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
2011-07-09 13:47:46 +08:00
|
|
|
// Find feature in table.
|
|
|
|
const SubtargetFeatureKV *FeatureEntry =
|
2014-05-07 04:23:04 +08:00
|
|
|
Find(StripFlag(Feature), FeatureTable);
|
2011-07-09 13:47:46 +08:00
|
|
|
// If there is a match
|
|
|
|
if (FeatureEntry) {
|
|
|
|
if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
|
|
|
|
Bits &= ~FeatureEntry->Value;
|
|
|
|
// For each feature that implies this, clear it.
|
2017-02-21 09:27:29 +08:00
|
|
|
ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
|
2011-07-09 13:47:46 +08:00
|
|
|
} else {
|
|
|
|
Bits |= FeatureEntry->Value;
|
|
|
|
|
|
|
|
// For each feature that this implies, set it.
|
2017-02-21 09:27:29 +08:00
|
|
|
SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
|
2011-07-09 13:47:46 +08:00
|
|
|
}
|
2014-01-26 00:56:18 +08:00
|
|
|
} else {
|
2017-02-21 09:27:29 +08:00
|
|
|
errs() << "'" << Feature << "' is not a recognized feature for this target"
|
2014-01-26 00:56:18 +08:00
|
|
|
<< " (ignoring feature)\n";
|
2011-07-09 13:47:46 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-11 09:41:30 +08:00
|
|
|
|
2016-01-05 18:25:56 +08:00
|
|
|
void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
|
2015-06-05 21:29:24 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
|
|
|
assert(hasFlag(Feature));
|
|
|
|
|
|
|
|
// Find feature in table.
|
|
|
|
const SubtargetFeatureKV *FeatureEntry =
|
|
|
|
Find(StripFlag(Feature), FeatureTable);
|
|
|
|
// If there is a match
|
|
|
|
if (FeatureEntry) {
|
|
|
|
// Enable/disable feature in bits
|
|
|
|
if (isEnabled(Feature)) {
|
2016-01-05 18:25:56 +08:00
|
|
|
Bits |= FeatureEntry->Value;
|
2015-06-05 21:29:24 +08:00
|
|
|
|
|
|
|
// For each feature that this implies, set it.
|
2017-02-21 09:27:29 +08:00
|
|
|
SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
|
2015-06-05 21:29:24 +08:00
|
|
|
} else {
|
|
|
|
Bits &= ~FeatureEntry->Value;
|
|
|
|
|
|
|
|
// For each feature that implies this, clear it.
|
2017-02-21 09:27:29 +08:00
|
|
|
ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
|
2015-06-05 21:29:24 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-21 09:27:29 +08:00
|
|
|
errs() << "'" << Feature << "' is not a recognized feature for this target"
|
2015-06-05 21:29:24 +08:00
|
|
|
<< " (ignoring feature)\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 18:47:10 +08:00
|
|
|
FeatureBitset
|
2014-08-31 00:48:02 +08:00
|
|
|
SubtargetFeatures::getFeatureBits(StringRef CPU,
|
2014-05-07 04:23:04 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> CPUTable,
|
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
|
|
|
if (CPUTable.empty() || FeatureTable.empty())
|
2015-05-26 18:47:10 +08:00
|
|
|
return FeatureBitset();
|
2011-07-07 15:07:08 +08:00
|
|
|
|
2016-01-03 15:33:45 +08:00
|
|
|
assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
|
|
|
|
"CPU table is not sorted");
|
|
|
|
assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
|
|
|
|
"CPU features table is not sorted");
|
2015-05-26 18:47:10 +08:00
|
|
|
// Resulting bits
|
|
|
|
FeatureBitset Bits;
|
2005-10-24 06:23:13 +08:00
|
|
|
|
2005-10-25 23:15:28 +08:00
|
|
|
// Check if help is needed
|
2011-06-30 09:53:36 +08:00
|
|
|
if (CPU == "help")
|
2014-05-07 04:23:04 +08:00
|
|
|
Help(CPUTable, FeatureTable);
|
2012-05-11 09:41:30 +08:00
|
|
|
|
2011-07-07 15:07:08 +08:00
|
|
|
// Find CPU entry if CPU name is specified.
|
2014-05-07 00:29:50 +08:00
|
|
|
else if (!CPU.empty()) {
|
2014-05-07 04:23:04 +08:00
|
|
|
const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
|
|
|
|
|
2011-07-07 15:07:08 +08:00
|
|
|
// If there is a match
|
|
|
|
if (CPUEntry) {
|
|
|
|
// Set base feature bits
|
|
|
|
Bits = CPUEntry->Value;
|
2007-06-28 07:34:06 +08:00
|
|
|
|
2011-07-07 15:07:08 +08:00
|
|
|
// Set the feature implied by this CPU feature, if any.
|
2014-05-07 04:23:04 +08:00
|
|
|
for (auto &FE : FeatureTable) {
|
2015-05-26 18:47:10 +08:00
|
|
|
if ((CPUEntry->Value & FE.Value).any())
|
2017-02-21 09:27:29 +08:00
|
|
|
SetImpliedBits(Bits, FE, FeatureTable);
|
2011-07-07 15:07:08 +08:00
|
|
|
}
|
2014-01-26 00:56:18 +08:00
|
|
|
} else {
|
2017-02-21 09:27:29 +08:00
|
|
|
errs() << "'" << CPU << "' is not a recognized processor for this target"
|
2014-01-26 00:56:18 +08:00
|
|
|
<< " (ignoring processor)\n";
|
2007-06-28 07:34:06 +08:00
|
|
|
}
|
2005-09-02 05:36:18 +08:00
|
|
|
}
|
2011-07-07 15:07:08 +08:00
|
|
|
|
2005-09-02 05:36:18 +08:00
|
|
|
// Iterate through each feature
|
2017-02-21 09:27:29 +08:00
|
|
|
for (const std::string &Feature : Features) {
|
2005-09-03 03:27:43 +08:00
|
|
|
// Check for help
|
2005-10-24 06:23:13 +08:00
|
|
|
if (Feature == "+help")
|
2014-05-07 04:23:04 +08:00
|
|
|
Help(CPUTable, FeatureTable);
|
2012-05-11 09:41:30 +08:00
|
|
|
|
2016-01-05 18:25:56 +08:00
|
|
|
ApplyFeatureFlag(Bits, Feature, FeatureTable);
|
2005-09-02 05:36:18 +08:00
|
|
|
}
|
2007-05-05 04:38:40 +08:00
|
|
|
|
2005-09-02 05:36:18 +08:00
|
|
|
return Bits;
|
|
|
|
}
|
|
|
|
|
2009-08-24 05:41:43 +08:00
|
|
|
void SubtargetFeatures::print(raw_ostream &OS) const {
|
2014-05-07 05:20:29 +08:00
|
|
|
for (auto &F : Features)
|
|
|
|
OS << F << " ";
|
2005-09-02 05:36:18 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-30 04:50:44 +08:00
|
|
|
LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
|
2010-01-05 09:29:36 +08:00
|
|
|
print(dbgs());
|
2005-09-02 05:36:18 +08:00
|
|
|
}
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2009-11-19 04:20:05 +08:00
|
|
|
|
2011-06-30 09:53:36 +08:00
|
|
|
void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
|
2017-02-21 09:27:29 +08:00
|
|
|
// FIXME: This is an inelegant way of specifying the features of a
|
|
|
|
// subtarget. It would be better if we could encode this information
|
|
|
|
// into the IR. See <rdar://5972456>.
|
2010-05-12 04:46:04 +08:00
|
|
|
if (Triple.getVendor() == Triple::Apple) {
|
|
|
|
if (Triple.getArch() == Triple::ppc) {
|
|
|
|
// powerpc-apple-*
|
|
|
|
AddFeature("altivec");
|
|
|
|
} else if (Triple.getArch() == Triple::ppc64) {
|
|
|
|
// powerpc64-apple-*
|
|
|
|
AddFeature("64bit");
|
|
|
|
AddFeature("altivec");
|
2009-11-19 04:20:05 +08:00
|
|
|
}
|
2010-05-11 08:30:02 +08:00
|
|
|
}
|
2009-11-19 04:20:05 +08:00
|
|
|
}
|