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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the SubtargetFeature interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-06-29 09:14:12 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.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>
|
2005-09-02 10:51:42 +08:00
|
|
|
#include <cctype>
|
2010-12-20 04:43:38 +08:00
|
|
|
#include <cstdlib>
|
2005-09-02 05:36:18 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2005-10-23 13:26:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Static Helper Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// hasFlag - 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 =='-';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// StripFlag - 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isEnabled - 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 == '+';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split - 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;
|
2014-05-14 03:55:17 +08:00
|
|
|
S.split(Tmp, ",", -1, false /* KeepEmpty */);
|
|
|
|
V.assign(Tmp.begin(), Tmp.end());
|
2005-09-02 05:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Join a vector of strings to a string with a comma separating each element.
|
2005-10-23 13:26:26 +08:00
|
|
|
///
|
|
|
|
static std::string Join(const std::vector<std::string> &V) {
|
2005-09-02 05:36:18 +08:00
|
|
|
// Start with empty string.
|
|
|
|
std::string Result;
|
2012-05-11 09:41:30 +08:00
|
|
|
// If the vector is not empty
|
2005-09-02 05:36:18 +08:00
|
|
|
if (!V.empty()) {
|
2011-06-30 09:53:36 +08:00
|
|
|
// Start with the first feature
|
2005-09-02 05:36:18 +08:00
|
|
|
Result = V[0];
|
|
|
|
// For each successive feature
|
|
|
|
for (size_t i = 1; i < V.size(); i++) {
|
|
|
|
// Add a comma
|
|
|
|
Result += ",";
|
|
|
|
// Add the feature
|
|
|
|
Result += V[i];
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 09:41:30 +08:00
|
|
|
// Return the features string
|
2005-09-02 05:36:18 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adding features.
|
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;
|
|
|
|
}
|
|
|
|
|
2005-10-24 06:23:13 +08:00
|
|
|
/// getLongestEntryLength - Return the length of the longest entry in the table.
|
2005-10-23 13:26:26 +08:00
|
|
|
///
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SubtargetFeatures Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-10-23 13:26:26 +08:00
|
|
|
|
2011-07-01 12:40:50 +08:00
|
|
|
std::string SubtargetFeatures::getString() const {
|
2005-10-23 13:26:26 +08:00
|
|
|
return Join(Features);
|
|
|
|
}
|
|
|
|
|
2007-05-05 04:38:40 +08:00
|
|
|
/// SetImpliedBits - For each feature that is (transitively) implied by this
|
|
|
|
/// feature, set it.
|
2009-05-24 03:50:50 +08:00
|
|
|
///
|
2007-05-05 04:38:40 +08:00
|
|
|
static
|
2015-05-13 16:27:08 +08:00
|
|
|
void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry,
|
2014-05-07 04:23:04 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
|
|
|
for (auto &FE : FeatureTable) {
|
2007-05-05 04:38:40 +08:00
|
|
|
if (FeatureEntry->Value == FE.Value) continue;
|
|
|
|
|
2015-05-13 16:27:08 +08:00
|
|
|
if ((FeatureEntry->Implies & FE.Value).any()) {
|
2007-05-05 04:38:40 +08:00
|
|
|
Bits |= FE.Value;
|
2014-05-07 04:23:04 +08:00
|
|
|
SetImpliedBits(Bits, &FE, FeatureTable);
|
2007-05-05 04:38:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ClearImpliedBits - For each feature that (transitively) implies this
|
|
|
|
/// feature, clear it.
|
2012-05-11 09:41:30 +08:00
|
|
|
///
|
2007-05-05 04:38:40 +08:00
|
|
|
static
|
2015-05-13 16:27:08 +08:00
|
|
|
void ClearImpliedBits(FeatureBitset &Bits,
|
|
|
|
const SubtargetFeatureKV *FeatureEntry,
|
2014-05-07 04:23:04 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
|
|
|
for (auto &FE : FeatureTable) {
|
2007-05-05 04:38:40 +08:00
|
|
|
if (FeatureEntry->Value == FE.Value) continue;
|
|
|
|
|
2015-05-13 16:27:08 +08:00
|
|
|
if ((FE.Implies & FeatureEntry->Value).any()) {
|
2007-05-05 04:38:40 +08:00
|
|
|
Bits &= ~FE.Value;
|
2014-05-07 04:23:04 +08:00
|
|
|
ClearImpliedBits(Bits, &FE, FeatureTable);
|
2007-05-05 04:38:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-25 23:15:28 +08:00
|
|
|
|
2011-07-09 13:47:46 +08:00
|
|
|
/// ToggleFeature - Toggle a feature and returns the newly updated feature
|
|
|
|
/// bits.
|
2015-05-13 16:27:08 +08:00
|
|
|
FeatureBitset
|
|
|
|
SubtargetFeatures::ToggleFeature(FeatureBitset Bits, StringRef Feature,
|
2014-05-07 05:04:27 +08:00
|
|
|
ArrayRef<SubtargetFeatureKV> FeatureTable) {
|
2014-05-07 04:23:04 +08:00
|
|
|
|
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.
|
2014-05-07 04:23:04 +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.
|
2014-05-07 04:23:04 +08:00
|
|
|
SetImpliedBits(Bits, FeatureEntry, FeatureTable);
|
2011-07-09 13:47:46 +08:00
|
|
|
}
|
2014-01-26 00:56:18 +08:00
|
|
|
} else {
|
|
|
|
errs() << "'" << Feature
|
|
|
|
<< "' is not a recognized feature for this target"
|
|
|
|
<< " (ignoring feature)\n";
|
2011-07-09 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Bits;
|
|
|
|
}
|
2012-05-11 09:41:30 +08:00
|
|
|
|
2011-07-09 13:47:46 +08:00
|
|
|
|
2011-06-30 09:53:36 +08:00
|
|
|
/// getFeatureBits - Get feature bits a CPU.
|
2005-10-23 13:26:26 +08:00
|
|
|
///
|
2015-05-13 16:27:08 +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-13 16:27:08 +08:00
|
|
|
return FeatureBitset();
|
2011-07-07 15:07:08 +08:00
|
|
|
|
2005-09-02 05:36:18 +08:00
|
|
|
#ifndef NDEBUG
|
2014-05-07 04:23:04 +08:00
|
|
|
for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
|
2005-09-02 05:36:18 +08:00
|
|
|
assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
|
|
|
|
"CPU table is not sorted");
|
|
|
|
}
|
2014-05-07 04:23:04 +08:00
|
|
|
for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
|
2005-09-02 05:36:18 +08:00
|
|
|
assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
|
|
|
|
"CPU features table is not sorted");
|
|
|
|
}
|
|
|
|
#endif
|
2015-05-13 16:27:08 +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-13 16:27:08 +08:00
|
|
|
if ((CPUEntry->Value & FE.Value).any())
|
2014-05-07 04:23:04 +08:00
|
|
|
SetImpliedBits(Bits, &FE, FeatureTable);
|
2011-07-07 15:07:08 +08:00
|
|
|
}
|
2014-01-26 00:56:18 +08:00
|
|
|
} else {
|
|
|
|
errs() << "'" << CPU
|
|
|
|
<< "' is not a recognized processor for this target"
|
|
|
|
<< " (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
|
2014-05-07 04:23:04 +08:00
|
|
|
for (auto &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
|
|
|
|
2005-09-02 05:36:18 +08:00
|
|
|
// Find feature in table.
|
|
|
|
const SubtargetFeatureKV *FeatureEntry =
|
2014-05-07 04:23:04 +08:00
|
|
|
Find(StripFlag(Feature), FeatureTable);
|
2005-09-02 05:36:18 +08:00
|
|
|
// If there is a match
|
|
|
|
if (FeatureEntry) {
|
|
|
|
// Enable/disable feature in bits
|
2007-05-05 04:38:40 +08:00
|
|
|
if (isEnabled(Feature)) {
|
|
|
|
Bits |= FeatureEntry->Value;
|
|
|
|
|
|
|
|
// For each feature that this implies, set it.
|
2014-05-07 04:23:04 +08:00
|
|
|
SetImpliedBits(Bits, FeatureEntry, FeatureTable);
|
2007-05-05 04:38:40 +08:00
|
|
|
} else {
|
|
|
|
Bits &= ~FeatureEntry->Value;
|
|
|
|
|
|
|
|
// For each feature that implies this, clear it.
|
2014-05-07 04:23:04 +08:00
|
|
|
ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
|
2007-05-05 04:38:40 +08:00
|
|
|
}
|
2014-01-26 00:56:18 +08:00
|
|
|
} else {
|
|
|
|
errs() << "'" << Feature
|
|
|
|
<< "' is not a recognized feature for this target"
|
|
|
|
<< " (ignoring feature)\n";
|
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;
|
|
|
|
}
|
|
|
|
|
2005-10-25 23:15:28 +08:00
|
|
|
/// print - Print feature string.
|
|
|
|
///
|
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";
|
|
|
|
}
|
|
|
|
|
2012-09-12 13:06:18 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2005-10-25 23:15:28 +08:00
|
|
|
/// dump - Dump feature info.
|
|
|
|
///
|
2005-09-02 05:36:18 +08:00
|
|
|
void SubtargetFeatures::dump() const {
|
2010-01-05 09:29:36 +08:00
|
|
|
print(dbgs());
|
2005-09-02 05:36:18 +08:00
|
|
|
}
|
2012-09-07 03:55:56 +08:00
|
|
|
#endif
|
2009-11-19 04:20:05 +08:00
|
|
|
|
2013-10-07 21:34:05 +08:00
|
|
|
/// Adds the default features for the specified target triple.
|
2009-11-19 04:20:05 +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>.
|
|
|
|
///
|
2011-06-30 09:53:36 +08:00
|
|
|
void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
|
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
|
|
|
}
|