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, const SubtargetFeatureKV *A,
110 // Determine the end of the array
111 const SubtargetFeatureKV *Hi = A + L;
112 // Binary search the array
113 const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S);
114 // If not found then return NULL
115 if (F == Hi || StringRef(F->Key) != S) return nullptr;
116 // Return the found array item
120 /// getLongestEntryLength - Return the length of the longest entry in the table.
122 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
125 for (size_t i = 0; i < Size; i++)
126 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
130 /// Display help for feature choices.
132 static uint64_t Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
133 const SubtargetFeatureKV *FeatTable,
134 size_t FeatTableSize) {
135 // Determine the length of the longest CPU and Feature entries.
136 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
137 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
139 // Print the CPU table.
140 errs() << "Available CPUs for this target:\n\n";
141 for (size_t i = 0; i != CPUTableSize; i++)
142 errs() << format(" %-*s - %s.\n",
143 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
146 // Print the Feature table.
147 errs() << "Available features for this target:\n\n";
148 for (size_t i = 0; i != FeatTableSize; i++)
149 errs() << format(" %-*s - %s.\n",
150 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
153 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
154 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
159 //===----------------------------------------------------------------------===//
160 // SubtargetFeatures Implementation
161 //===----------------------------------------------------------------------===//
163 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
164 // Break up string into separate features
165 Split(Features, Initial);
169 std::string SubtargetFeatures::getString() const {
170 return Join(Features);
173 /// SetImpliedBits - For each feature that is (transitively) implied by this
177 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
178 const SubtargetFeatureKV *FeatureTable,
179 size_t FeatureTableSize) {
180 for (size_t i = 0; i < FeatureTableSize; ++i) {
181 const SubtargetFeatureKV &FE = FeatureTable[i];
183 if (FeatureEntry->Value == FE.Value) continue;
185 if (FeatureEntry->Implies & FE.Value) {
187 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
192 /// ClearImpliedBits - For each feature that (transitively) implies this
193 /// feature, clear it.
196 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
197 const SubtargetFeatureKV *FeatureTable,
198 size_t FeatureTableSize) {
199 for (size_t i = 0; i < FeatureTableSize; ++i) {
200 const SubtargetFeatureKV &FE = FeatureTable[i];
202 if (FeatureEntry->Value == FE.Value) continue;
204 if (FE.Implies & FeatureEntry->Value) {
206 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
211 /// ToggleFeature - Toggle a feature and returns the newly updated feature
214 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
215 const SubtargetFeatureKV *FeatureTable,
216 size_t FeatureTableSize) {
217 // Find feature in table.
218 const SubtargetFeatureKV *FeatureEntry =
219 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
220 // If there is a match
222 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
223 Bits &= ~FeatureEntry->Value;
225 // For each feature that implies this, clear it.
226 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
228 Bits |= FeatureEntry->Value;
230 // For each feature that this implies, set it.
231 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
234 errs() << "'" << Feature
235 << "' is not a recognized feature for this target"
236 << " (ignoring feature)\n";
243 /// getFeatureBits - Get feature bits a CPU.
245 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
246 const SubtargetFeatureKV *CPUTable,
248 const SubtargetFeatureKV *FeatureTable,
249 size_t FeatureTableSize) {
250 if (!FeatureTableSize || !CPUTableSize)
254 for (size_t i = 1; i < CPUTableSize; i++) {
255 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
256 "CPU table is not sorted");
258 for (size_t i = 1; i < FeatureTableSize; i++) {
259 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
260 "CPU features table is not sorted");
263 uint64_t Bits = 0; // Resulting bits
265 // Check if help is needed
267 return Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
269 // Find CPU entry if CPU name is specified.
271 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
272 // If there is a match
274 // Set base feature bits
275 Bits = CPUEntry->Value;
277 // Set the feature implied by this CPU feature, if any.
278 for (size_t i = 0; i < FeatureTableSize; ++i) {
279 const SubtargetFeatureKV &FE = FeatureTable[i];
280 if (CPUEntry->Value & FE.Value)
281 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
285 << "' is not a recognized processor for this target"
286 << " (ignoring processor)\n";
290 // Iterate through each feature
291 for (size_t i = 0, E = Features.size(); i < E; i++) {
292 const StringRef Feature = Features[i];
295 if (Feature == "+help")
296 return Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
298 // Find feature in table.
299 const SubtargetFeatureKV *FeatureEntry =
300 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
301 // If there is a match
303 // Enable/disable feature in bits
304 if (isEnabled(Feature)) {
305 Bits |= FeatureEntry->Value;
307 // For each feature that this implies, set it.
308 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
310 Bits &= ~FeatureEntry->Value;
312 // For each feature that implies this, clear it.
313 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
316 errs() << "'" << Feature
317 << "' is not a recognized feature for this target"
318 << " (ignoring feature)\n";
325 /// print - Print feature string.
327 void SubtargetFeatures::print(raw_ostream &OS) const {
328 for (size_t i = 0, e = Features.size(); i != e; ++i)
329 OS << Features[i] << " ";
333 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
334 /// dump - Dump feature info.
336 void SubtargetFeatures::dump() const {
341 /// Adds the default features for the specified target triple.
343 /// FIXME: This is an inelegant way of specifying the features of a
344 /// subtarget. It would be better if we could encode this information
345 /// into the IR. See <rdar://5972456>.
347 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
348 if (Triple.getVendor() == Triple::Apple) {
349 if (Triple.getArch() == Triple::ppc) {
351 AddFeature("altivec");
352 } else if (Triple.getArch() == Triple::ppc64) {
355 AddFeature("altivec");