1 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SubtargetFeature interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/SubtargetFeature.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
24 //===----------------------------------------------------------------------===//
25 // Static Helper Functions
26 //===----------------------------------------------------------------------===//
28 /// hasFlag - Determine if a feature has a flag; '+' or '-'
30 static inline bool hasFlag(const StringRef Feature) {
31 assert(!Feature.empty() && "Empty string");
32 // Get first character
34 // Check if first character is '+' or '-' flag
35 return Ch == '+' || Ch =='-';
38 /// StripFlag - Return string stripped of flag.
40 static inline std::string StripFlag(const StringRef Feature) {
41 return hasFlag(Feature) ? Feature.substr(1) : Feature;
44 /// isEnabled - Return true if enable flag; '+'.
46 static inline bool isEnabled(const StringRef Feature) {
47 assert(!Feature.empty() && "Empty string");
48 // Get first character
50 // Check if first character is '+' for enabled
54 /// Split - Splits a string of comma separated items in to a vector of strings.
56 static void Split(std::vector<std::string> &V, const StringRef S) {
60 // Start at beginning of string.
63 // Find the next comma
64 size_t Comma = S.find(',', Pos);
65 // If no comma found then the rest of the string is used
66 if (Comma == std::string::npos) {
67 // Add string to vector
68 V.push_back(S.substr(Pos));
71 // Otherwise add substring to vector
72 V.push_back(S.substr(Pos, Comma - Pos));
73 // Advance to next item
78 /// Join a vector of strings to a string with a comma separating each element.
80 static std::string Join(const std::vector<std::string> &V) {
81 // Start with empty string.
83 // If the vector is not empty
85 // Start with the first feature
87 // For each successive feature
88 for (size_t i = 1; i < V.size(); i++) {
95 // Return the features string
100 void SubtargetFeatures::AddFeature(const StringRef String) {
101 // Don't add empty features or features we already have.
103 // Convert to lowercase, prepend flag if we don't already have a flag.
104 Features.push_back(hasFlag(String) ? String.str() : "+" + String.lower());
107 /// Find KV in array using binary search.
108 static const SubtargetFeatureKV *Find(StringRef S,
109 ArrayRef<SubtargetFeatureKV> A) {
110 // Binary search the array
111 auto F = std::lower_bound(A.begin(), A.end(), S);
112 // If not found then return NULL
113 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
114 // Return the found array item
118 /// getLongestEntryLength - Return the length of the longest entry in the table.
120 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
122 for (auto &I : Table)
123 MaxLen = std::max(MaxLen, std::strlen(I.Key));
127 /// Display help for feature choices.
129 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
130 ArrayRef<SubtargetFeatureKV> FeatTable) {
131 // Determine the length of the longest CPU and Feature entries.
132 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
133 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
135 // Print the CPU table.
136 errs() << "Available CPUs for this target:\n\n";
137 for (auto &CPU : CPUTable)
138 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
141 // Print the Feature table.
142 errs() << "Available features for this target:\n\n";
143 for (auto &Feature : FeatTable)
144 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
147 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
148 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
151 //===----------------------------------------------------------------------===//
152 // SubtargetFeatures Implementation
153 //===----------------------------------------------------------------------===//
155 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
156 // Break up string into separate features
157 Split(Features, Initial);
161 std::string SubtargetFeatures::getString() const {
162 return Join(Features);
165 /// SetImpliedBits - For each feature that is (transitively) implied by this
169 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
170 ArrayRef<SubtargetFeatureKV> FeatureTable) {
171 for (auto &FE : FeatureTable) {
172 if (FeatureEntry->Value == FE.Value) continue;
174 if (FeatureEntry->Implies & FE.Value) {
176 SetImpliedBits(Bits, &FE, FeatureTable);
181 /// ClearImpliedBits - For each feature that (transitively) implies this
182 /// feature, clear it.
185 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
186 ArrayRef<SubtargetFeatureKV> FeatureTable) {
187 for (auto &FE : FeatureTable) {
188 if (FeatureEntry->Value == FE.Value) continue;
190 if (FE.Implies & FeatureEntry->Value) {
192 ClearImpliedBits(Bits, &FE, FeatureTable);
197 /// ToggleFeature - Toggle a feature and returns the newly updated feature
200 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
201 ArrayRef<SubtargetFeatureKV> FeatureTable) {
203 // Find feature in table.
204 const SubtargetFeatureKV *FeatureEntry =
205 Find(StripFlag(Feature), FeatureTable);
206 // If there is a match
208 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
209 Bits &= ~FeatureEntry->Value;
211 // For each feature that implies this, clear it.
212 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
214 Bits |= FeatureEntry->Value;
216 // For each feature that this implies, set it.
217 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
220 errs() << "'" << Feature
221 << "' is not a recognized feature for this target"
222 << " (ignoring feature)\n";
229 /// getFeatureBits - Get feature bits a CPU.
232 SubtargetFeatures::getFeatureBits(const StringRef CPU,
233 ArrayRef<SubtargetFeatureKV> CPUTable,
234 ArrayRef<SubtargetFeatureKV> FeatureTable) {
236 if (CPUTable.empty() || FeatureTable.empty())
240 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
241 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
242 "CPU table is not sorted");
244 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
245 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
246 "CPU features table is not sorted");
249 uint64_t Bits = 0; // Resulting bits
251 // Check if help is needed
253 Help(CPUTable, FeatureTable);
255 // Find CPU entry if CPU name is specified.
256 else if (!CPU.empty()) {
257 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
259 // If there is a match
261 // Set base feature bits
262 Bits = CPUEntry->Value;
264 // Set the feature implied by this CPU feature, if any.
265 for (auto &FE : FeatureTable) {
266 if (CPUEntry->Value & FE.Value)
267 SetImpliedBits(Bits, &FE, FeatureTable);
271 << "' is not a recognized processor for this target"
272 << " (ignoring processor)\n";
276 // Iterate through each feature
277 for (auto &Feature : Features) {
279 if (Feature == "+help")
280 Help(CPUTable, FeatureTable);
282 // Find feature in table.
283 const SubtargetFeatureKV *FeatureEntry =
284 Find(StripFlag(Feature), FeatureTable);
285 // If there is a match
287 // Enable/disable feature in bits
288 if (isEnabled(Feature)) {
289 Bits |= FeatureEntry->Value;
291 // For each feature that this implies, set it.
292 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
294 Bits &= ~FeatureEntry->Value;
296 // For each feature that implies this, clear it.
297 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
300 errs() << "'" << Feature
301 << "' is not a recognized feature for this target"
302 << " (ignoring feature)\n";
309 /// print - Print feature string.
311 void SubtargetFeatures::print(raw_ostream &OS) const {
312 for (auto &F : Features)
317 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
318 /// dump - Dump feature info.
320 void SubtargetFeatures::dump() const {
325 /// Adds the default features for the specified target triple.
327 /// FIXME: This is an inelegant way of specifying the features of a
328 /// subtarget. It would be better if we could encode this information
329 /// into the IR. See <rdar://5972456>.
331 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
332 if (Triple.getVendor() == Triple::Apple) {
333 if (Triple.getArch() == Triple::ppc) {
335 AddFeature("altivec");
336 } else if (Triple.getArch() == Triple::ppc64) {
339 AddFeature("altivec");