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"
18 #include "llvm/ADT/StringExtras.h"
25 //===----------------------------------------------------------------------===//
26 // Static Helper Functions
27 //===----------------------------------------------------------------------===//
29 /// hasFlag - Determine if a feature has a flag; '+' or '-'
31 static inline bool hasFlag(const StringRef Feature) {
32 assert(!Feature.empty() && "Empty string");
33 // Get first character
35 // Check if first character is '+' or '-' flag
36 return Ch == '+' || Ch =='-';
39 /// StripFlag - Return string stripped of flag.
41 static inline std::string StripFlag(const StringRef Feature) {
42 return hasFlag(Feature) ? Feature.substr(1) : Feature;
45 /// isEnabled - Return true if enable flag; '+'.
47 static inline bool isEnabled(const StringRef Feature) {
48 assert(!Feature.empty() && "Empty string");
49 // Get first character
51 // Check if first character is '+' for enabled
55 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
57 static inline std::string PrependFlag(const StringRef Feature,
59 assert(!Feature.empty() && "Empty string");
62 std::string Prefix = IsEnabled ? "+" : "-";
67 /// Split - Splits a string of comma separated items in to a vector of strings.
69 static void Split(std::vector<std::string> &V, const StringRef S) {
73 // Start at beginning of string.
76 // Find the next comma
77 size_t Comma = S.find(',', Pos);
78 // If no comma found then the rest of the string is used
79 if (Comma == std::string::npos) {
80 // Add string to vector
81 V.push_back(S.substr(Pos));
84 // Otherwise add substring to vector
85 V.push_back(S.substr(Pos, Comma - Pos));
86 // Advance to next item
91 /// Join a vector of strings to a string with a comma separating each element.
93 static std::string Join(const std::vector<std::string> &V) {
94 // Start with empty string.
96 // If the vector is not empty
98 // Start with the first feature
100 // For each successive feature
101 for (size_t i = 1; i < V.size(); i++) {
108 // Return the features string
113 void SubtargetFeatures::AddFeature(const StringRef String,
115 // Don't add empty features
116 if (!String.empty()) {
117 // Convert to lowercase, prepend flag and add to vector
118 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
122 /// Find KV in array using binary search.
123 template<typename T> const T *Find(const StringRef S, const T *A, size_t L) {
124 // Make the lower bound element we're looking for
127 // Determine the end of the array
129 // Binary search the array
130 const T *F = std::lower_bound(A, Hi, KV);
131 // If not found then return NULL
132 if (F == Hi || StringRef(F->Key) != S) return NULL;
133 // Return the found array item
137 /// getLongestEntryLength - Return the length of the longest entry in the table.
139 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
142 for (size_t i = 0; i < Size; i++)
143 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
147 /// Display help for feature choices.
149 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
150 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
151 // Determine the length of the longest CPU and Feature entries.
152 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
153 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
155 // Print the CPU table.
156 errs() << "Available CPUs for this target:\n\n";
157 for (size_t i = 0; i != CPUTableSize; i++)
158 errs() << format(" %-*s - %s.\n",
159 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
162 // Print the Feature table.
163 errs() << "Available features for this target:\n\n";
164 for (size_t i = 0; i != FeatTableSize; i++)
165 errs() << format(" %-*s - %s.\n",
166 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
169 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
170 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
174 //===----------------------------------------------------------------------===//
175 // SubtargetFeatures Implementation
176 //===----------------------------------------------------------------------===//
178 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
179 // Break up string into separate features
180 Split(Features, Initial);
184 std::string SubtargetFeatures::getString() const {
185 return Join(Features);
188 /// SetImpliedBits - For each feature that is (transitively) implied by this
192 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
193 const SubtargetFeatureKV *FeatureTable,
194 size_t FeatureTableSize) {
195 for (size_t i = 0; i < FeatureTableSize; ++i) {
196 const SubtargetFeatureKV &FE = FeatureTable[i];
198 if (FeatureEntry->Value == FE.Value) continue;
200 if (FeatureEntry->Implies & FE.Value) {
202 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
207 /// ClearImpliedBits - For each feature that (transitively) implies this
208 /// feature, clear it.
211 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
212 const SubtargetFeatureKV *FeatureTable,
213 size_t FeatureTableSize) {
214 for (size_t i = 0; i < FeatureTableSize; ++i) {
215 const SubtargetFeatureKV &FE = FeatureTable[i];
217 if (FeatureEntry->Value == FE.Value) continue;
219 if (FE.Implies & FeatureEntry->Value) {
221 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
226 /// ToggleFeature - Toggle a feature and returns the newly updated feature
229 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
230 const SubtargetFeatureKV *FeatureTable,
231 size_t FeatureTableSize) {
232 // Find feature in table.
233 const SubtargetFeatureKV *FeatureEntry =
234 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
235 // If there is a match
237 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
238 Bits &= ~FeatureEntry->Value;
240 // For each feature that implies this, clear it.
241 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
243 Bits |= FeatureEntry->Value;
245 // For each feature that this implies, set it.
246 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
249 errs() << "'" << Feature
250 << "' is not a recognized feature for this target"
251 << " (ignoring feature)\n";
258 /// getFeatureBits - Get feature bits a CPU.
260 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
261 const SubtargetFeatureKV *CPUTable,
263 const SubtargetFeatureKV *FeatureTable,
264 size_t FeatureTableSize) {
265 if (!FeatureTableSize || !CPUTableSize)
269 for (size_t i = 1; i < CPUTableSize; i++) {
270 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
271 "CPU table is not sorted");
273 for (size_t i = 1; i < FeatureTableSize; i++) {
274 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
275 "CPU features table is not sorted");
278 uint64_t Bits = 0; // Resulting bits
280 // Check if help is needed
282 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
284 // Find CPU entry if CPU name is specified.
286 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
287 // If there is a match
289 // Set base feature bits
290 Bits = CPUEntry->Value;
292 // Set the feature implied by this CPU feature, if any.
293 for (size_t i = 0; i < FeatureTableSize; ++i) {
294 const SubtargetFeatureKV &FE = FeatureTable[i];
295 if (CPUEntry->Value & FE.Value)
296 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
300 << "' is not a recognized processor for this target"
301 << " (ignoring processor)\n";
305 // Iterate through each feature
306 for (size_t i = 0, E = Features.size(); i < E; i++) {
307 const StringRef Feature = Features[i];
310 if (Feature == "+help")
311 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
313 // Find feature in table.
314 const SubtargetFeatureKV *FeatureEntry =
315 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
316 // If there is a match
318 // Enable/disable feature in bits
319 if (isEnabled(Feature)) {
320 Bits |= FeatureEntry->Value;
322 // For each feature that this implies, set it.
323 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
325 Bits &= ~FeatureEntry->Value;
327 // For each feature that implies this, clear it.
328 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
331 errs() << "'" << Feature
332 << "' is not a recognized feature for this target"
333 << " (ignoring feature)\n";
340 /// Get scheduling itinerary of a CPU.
341 void *SubtargetFeatures::getItinerary(const StringRef CPU,
342 const SubtargetInfoKV *Table,
344 assert(Table && "missing table");
346 for (size_t i = 1; i < TableSize; i++) {
347 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
352 const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize);
358 << "' is not a recognized processor for this target"
359 << " (ignoring processor)\n";
364 /// print - Print feature string.
366 void SubtargetFeatures::print(raw_ostream &OS) const {
367 for (size_t i = 0, e = Features.size(); i != e; ++i)
368 OS << Features[i] << " ";
372 /// dump - Dump feature info.
374 void SubtargetFeatures::dump() const {
378 /// getDefaultSubtargetFeatures - Return a string listing the features
379 /// associated with the target triple.
381 /// FIXME: This is an inelegant way of specifying the features of a
382 /// subtarget. It would be better if we could encode this information
383 /// into the IR. See <rdar://5972456>.
385 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
386 if (Triple.getVendor() == Triple::Apple) {
387 if (Triple.getArch() == Triple::ppc) {
389 AddFeature("altivec");
390 } else if (Triple.getArch() == Triple::ppc64) {
393 AddFeature("altivec");