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 void 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";
157 //===----------------------------------------------------------------------===//
158 // SubtargetFeatures Implementation
159 //===----------------------------------------------------------------------===//
161 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
162 // Break up string into separate features
163 Split(Features, Initial);
167 std::string SubtargetFeatures::getString() const {
168 return Join(Features);
171 /// SetImpliedBits - For each feature that is (transitively) implied by this
175 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
176 const SubtargetFeatureKV *FeatureTable,
177 size_t FeatureTableSize) {
178 for (size_t i = 0; i < FeatureTableSize; ++i) {
179 const SubtargetFeatureKV &FE = FeatureTable[i];
181 if (FeatureEntry->Value == FE.Value) continue;
183 if (FeatureEntry->Implies & FE.Value) {
185 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
190 /// ClearImpliedBits - For each feature that (transitively) implies this
191 /// feature, clear it.
194 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
195 const SubtargetFeatureKV *FeatureTable,
196 size_t FeatureTableSize) {
197 for (size_t i = 0; i < FeatureTableSize; ++i) {
198 const SubtargetFeatureKV &FE = FeatureTable[i];
200 if (FeatureEntry->Value == FE.Value) continue;
202 if (FE.Implies & FeatureEntry->Value) {
204 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
209 /// ToggleFeature - Toggle a feature and returns the newly updated feature
212 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
213 const SubtargetFeatureKV *FeatureTable,
214 size_t FeatureTableSize) {
215 // Find feature in table.
216 const SubtargetFeatureKV *FeatureEntry =
217 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
218 // If there is a match
220 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
221 Bits &= ~FeatureEntry->Value;
223 // For each feature that implies this, clear it.
224 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
226 Bits |= FeatureEntry->Value;
228 // For each feature that this implies, set it.
229 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
232 errs() << "'" << Feature
233 << "' is not a recognized feature for this target"
234 << " (ignoring feature)\n";
241 /// getFeatureBits - Get feature bits a CPU.
243 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
244 const SubtargetFeatureKV *CPUTable,
246 const SubtargetFeatureKV *FeatureTable,
247 size_t FeatureTableSize) {
248 if (!FeatureTableSize || !CPUTableSize)
252 for (size_t i = 1; i < CPUTableSize; i++) {
253 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
254 "CPU table is not sorted");
256 for (size_t i = 1; i < FeatureTableSize; i++) {
257 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
258 "CPU features table is not sorted");
261 uint64_t Bits = 0; // Resulting bits
263 // Check if help is needed
265 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
267 // Find CPU entry if CPU name is specified.
268 else if (!CPU.empty()) {
269 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
270 // If there is a match
272 // Set base feature bits
273 Bits = CPUEntry->Value;
275 // Set the feature implied by this CPU feature, if any.
276 for (size_t i = 0; i < FeatureTableSize; ++i) {
277 const SubtargetFeatureKV &FE = FeatureTable[i];
278 if (CPUEntry->Value & FE.Value)
279 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
283 << "' is not a recognized processor for this target"
284 << " (ignoring processor)\n";
288 // Iterate through each feature
289 for (size_t i = 0, E = Features.size(); i < E; i++) {
290 const StringRef Feature = Features[i];
293 if (Feature == "+help")
294 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
296 // Find feature in table.
297 const SubtargetFeatureKV *FeatureEntry =
298 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
299 // If there is a match
301 // Enable/disable feature in bits
302 if (isEnabled(Feature)) {
303 Bits |= FeatureEntry->Value;
305 // For each feature that this implies, set it.
306 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
308 Bits &= ~FeatureEntry->Value;
310 // For each feature that implies this, clear it.
311 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
314 errs() << "'" << Feature
315 << "' is not a recognized feature for this target"
316 << " (ignoring feature)\n";
323 /// print - Print feature string.
325 void SubtargetFeatures::print(raw_ostream &OS) const {
326 for (size_t i = 0, e = Features.size(); i != e; ++i)
327 OS << Features[i] << " ";
331 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
332 /// dump - Dump feature info.
334 void SubtargetFeatures::dump() const {
339 /// Adds the default features for the specified target triple.
341 /// FIXME: This is an inelegant way of specifying the features of a
342 /// subtarget. It would be better if we could encode this information
343 /// into the IR. See <rdar://5972456>.
345 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
346 if (Triple.getVendor() == Triple::Apple) {
347 if (Triple.getArch() == Triple::ppc) {
349 AddFeature("altivec");
350 } else if (Triple.getArch() == Triple::ppc64) {
353 AddFeature("altivec");