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(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(StringRef Feature) {
41 return hasFlag(Feature) ? Feature.substr(1) : Feature;
44 /// isEnabled - Return true if enable flag; '+'.
46 static inline bool isEnabled(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, StringRef S) {
57 SmallVector<StringRef, 3> Tmp;
58 S.split(Tmp, ",", -1, false /* KeepEmpty */);
59 V.assign(Tmp.begin(), Tmp.end());
62 /// Join a vector of strings to a string with a comma separating each element.
64 static std::string Join(const std::vector<std::string> &V) {
65 // Start with empty string.
67 // If the vector is not empty
69 // Start with the first feature
71 // For each successive feature
72 for (size_t i = 1; i < V.size(); i++) {
79 // Return the features string
84 void SubtargetFeatures::AddFeature(StringRef String) {
85 // Don't add empty features or features we already have.
87 // Convert to lowercase, prepend flag if we don't already have a flag.
88 Features.push_back(hasFlag(String) ? String.str() : "+" + String.lower());
91 /// Find KV in array using binary search.
92 static const SubtargetFeatureKV *Find(StringRef S,
93 ArrayRef<SubtargetFeatureKV> A) {
94 // Binary search the array
95 auto F = std::lower_bound(A.begin(), A.end(), S);
96 // If not found then return NULL
97 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
98 // Return the found array item
102 /// getLongestEntryLength - Return the length of the longest entry in the table.
104 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
106 for (auto &I : Table)
107 MaxLen = std::max(MaxLen, std::strlen(I.Key));
111 /// Display help for feature choices.
113 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
114 ArrayRef<SubtargetFeatureKV> FeatTable) {
115 // Determine the length of the longest CPU and Feature entries.
116 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
117 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
119 // Print the CPU table.
120 errs() << "Available CPUs for this target:\n\n";
121 for (auto &CPU : CPUTable)
122 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
125 // Print the Feature table.
126 errs() << "Available features for this target:\n\n";
127 for (auto &Feature : FeatTable)
128 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
131 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
132 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
135 //===----------------------------------------------------------------------===//
136 // SubtargetFeatures Implementation
137 //===----------------------------------------------------------------------===//
139 SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
140 // Break up string into separate features
141 Split(Features, Initial);
145 std::string SubtargetFeatures::getString() const {
146 return Join(Features);
149 /// SetImpliedBits - For each feature that is (transitively) implied by this
153 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
154 ArrayRef<SubtargetFeatureKV> FeatureTable) {
155 for (auto &FE : FeatureTable) {
156 if (FeatureEntry->Value == FE.Value) continue;
158 if (FeatureEntry->Implies & FE.Value) {
160 SetImpliedBits(Bits, &FE, FeatureTable);
165 /// ClearImpliedBits - For each feature that (transitively) implies this
166 /// feature, clear it.
169 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
170 ArrayRef<SubtargetFeatureKV> FeatureTable) {
171 for (auto &FE : FeatureTable) {
172 if (FeatureEntry->Value == FE.Value) continue;
174 if (FE.Implies & FeatureEntry->Value) {
176 ClearImpliedBits(Bits, &FE, FeatureTable);
181 /// ToggleFeature - Toggle a feature and returns the newly updated feature
184 SubtargetFeatures::ToggleFeature(uint64_t Bits, StringRef Feature,
185 ArrayRef<SubtargetFeatureKV> FeatureTable) {
187 // Find feature in table.
188 const SubtargetFeatureKV *FeatureEntry =
189 Find(StripFlag(Feature), FeatureTable);
190 // If there is a match
192 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
193 Bits &= ~FeatureEntry->Value;
195 // For each feature that implies this, clear it.
196 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
198 Bits |= FeatureEntry->Value;
200 // For each feature that this implies, set it.
201 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
204 errs() << "'" << Feature
205 << "' is not a recognized feature for this target"
206 << " (ignoring feature)\n";
213 /// getFeatureBits - Get feature bits a CPU.
216 SubtargetFeatures::getFeatureBits(StringRef CPU,
217 ArrayRef<SubtargetFeatureKV> CPUTable,
218 ArrayRef<SubtargetFeatureKV> FeatureTable) {
220 if (CPUTable.empty() || FeatureTable.empty())
224 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
225 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
226 "CPU table is not sorted");
228 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
229 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
230 "CPU features table is not sorted");
233 uint64_t Bits = 0; // Resulting bits
235 // Check if help is needed
237 Help(CPUTable, FeatureTable);
239 // Find CPU entry if CPU name is specified.
240 else if (!CPU.empty()) {
241 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
243 // If there is a match
245 // Set base feature bits
246 Bits = CPUEntry->Value;
248 // Set the feature implied by this CPU feature, if any.
249 for (auto &FE : FeatureTable) {
250 if (CPUEntry->Value & FE.Value)
251 SetImpliedBits(Bits, &FE, FeatureTable);
255 << "' is not a recognized processor for this target"
256 << " (ignoring processor)\n";
260 // Iterate through each feature
261 for (auto &Feature : Features) {
263 if (Feature == "+help")
264 Help(CPUTable, FeatureTable);
266 // Find feature in table.
267 const SubtargetFeatureKV *FeatureEntry =
268 Find(StripFlag(Feature), FeatureTable);
269 // If there is a match
271 // Enable/disable feature in bits
272 if (isEnabled(Feature)) {
273 Bits |= FeatureEntry->Value;
275 // For each feature that this implies, set it.
276 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
278 Bits &= ~FeatureEntry->Value;
280 // For each feature that implies this, clear it.
281 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
284 errs() << "'" << Feature
285 << "' is not a recognized feature for this target"
286 << " (ignoring feature)\n";
293 /// print - Print feature string.
295 void SubtargetFeatures::print(raw_ostream &OS) const {
296 for (auto &F : Features)
301 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
302 /// dump - Dump feature info.
304 void SubtargetFeatures::dump() const {
309 /// Adds the default features for the specified target triple.
311 /// FIXME: This is an inelegant way of specifying the features of a
312 /// subtarget. It would be better if we could encode this information
313 /// into the IR. See <rdar://5972456>.
315 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
316 if (Triple.getVendor() == Triple::Apple) {
317 if (Triple.getArch() == Triple::ppc) {
319 AddFeature("altivec");
320 } else if (Triple.getArch() == Triple::ppc64) {
323 AddFeature("altivec");