Use std::bitset for SubtargetFeatures
[oota-llvm.git] / lib / MC / SubtargetFeature.cpp
1 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SubtargetFeature interface.
11 //
12 //===----------------------------------------------------------------------===//
13
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 <algorithm>
19 #include <cassert>
20 #include <cctype>
21 #include <cstdlib>
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 //                          Static Helper Functions
26 //===----------------------------------------------------------------------===//
27
28 /// hasFlag - Determine if a feature has a flag; '+' or '-'
29 ///
30 static inline bool hasFlag(StringRef Feature) {
31   assert(!Feature.empty() && "Empty string");
32   // Get first character
33   char Ch = Feature[0];
34   // Check if first character is '+' or '-' flag
35   return Ch == '+' || Ch =='-';
36 }
37
38 /// StripFlag - Return string stripped of flag.
39 ///
40 static inline std::string StripFlag(StringRef Feature) {
41   return hasFlag(Feature) ? Feature.substr(1) : Feature;
42 }
43
44 /// isEnabled - Return true if enable flag; '+'.
45 ///
46 static inline bool isEnabled(StringRef Feature) {
47   assert(!Feature.empty() && "Empty string");
48   // Get first character
49   char Ch = Feature[0];
50   // Check if first character is '+' for enabled
51   return Ch == '+';
52 }
53
54 /// Split - Splits a string of comma separated items in to a vector of strings.
55 ///
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());
60 }
61
62 /// Join a vector of strings to a string with a comma separating each element.
63 ///
64 static std::string Join(const std::vector<std::string> &V) {
65   // Start with empty string.
66   std::string Result;
67   // If the vector is not empty
68   if (!V.empty()) {
69     // Start with the first feature
70     Result = V[0];
71     // For each successive feature
72     for (size_t i = 1; i < V.size(); i++) {
73       // Add a comma
74       Result += ",";
75       // Add the feature
76       Result += V[i];
77     }
78   }
79   // Return the features string
80   return Result;
81 }
82
83 /// Adding features.
84 void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
85   // Don't add empty features.
86   if (!String.empty())
87     // Convert to lowercase, prepend flag if we don't already have a flag.
88     Features.push_back(hasFlag(String) ? String.lower()
89                                        : (Enable ? "+" : "-") + String.lower());
90 }
91
92 /// Find KV in array using binary search.
93 static const SubtargetFeatureKV *Find(StringRef S,
94                                       ArrayRef<SubtargetFeatureKV> A) {
95   // Binary search the array
96   auto F = std::lower_bound(A.begin(), A.end(), S);
97   // If not found then return NULL
98   if (F == A.end() || StringRef(F->Key) != S) return nullptr;
99   // Return the found array item
100   return F;
101 }
102
103 /// getLongestEntryLength - Return the length of the longest entry in the table.
104 ///
105 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
106   size_t MaxLen = 0;
107   for (auto &I : Table)
108     MaxLen = std::max(MaxLen, std::strlen(I.Key));
109   return MaxLen;
110 }
111
112 /// Display help for feature choices.
113 ///
114 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
115                  ArrayRef<SubtargetFeatureKV> FeatTable) {
116   // Determine the length of the longest CPU and Feature entries.
117   unsigned MaxCPULen  = getLongestEntryLength(CPUTable);
118   unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
119
120   // Print the CPU table.
121   errs() << "Available CPUs for this target:\n\n";
122   for (auto &CPU : CPUTable)
123     errs() << format("  %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
124   errs() << '\n';
125
126   // Print the Feature table.
127   errs() << "Available features for this target:\n\n";
128   for (auto &Feature : FeatTable)
129     errs() << format("  %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
130   errs() << '\n';
131
132   errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
133             "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
134 }
135
136 //===----------------------------------------------------------------------===//
137 //                    SubtargetFeatures Implementation
138 //===----------------------------------------------------------------------===//
139
140 SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
141   // Break up string into separate features
142   Split(Features, Initial);
143 }
144
145
146 std::string SubtargetFeatures::getString() const {
147   return Join(Features);
148 }
149
150 /// SetImpliedBits - For each feature that is (transitively) implied by this
151 /// feature, set it.
152 ///
153 static
154 void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry,
155                     ArrayRef<SubtargetFeatureKV> FeatureTable) {
156   for (auto &FE : FeatureTable) {
157     if (FeatureEntry->Value == FE.Value) continue;
158
159     if ((FeatureEntry->Implies & FE.Value).any()) {
160       Bits |= FE.Value;
161       SetImpliedBits(Bits, &FE, FeatureTable);
162     }
163   }
164 }
165
166 /// ClearImpliedBits - For each feature that (transitively) implies this
167 /// feature, clear it.
168 ///
169 static
170 void ClearImpliedBits(FeatureBitset &Bits, 
171                       const SubtargetFeatureKV *FeatureEntry,
172                       ArrayRef<SubtargetFeatureKV> FeatureTable) {
173   for (auto &FE : FeatureTable) {
174     if (FeatureEntry->Value == FE.Value) continue;
175
176     if ((FE.Implies & FeatureEntry->Value).any()) {
177       Bits &= ~FE.Value;
178       ClearImpliedBits(Bits, &FE, FeatureTable);
179     }
180   }
181 }
182
183 /// ToggleFeature - Toggle a feature and returns the newly updated feature
184 /// bits.
185 FeatureBitset
186 SubtargetFeatures::ToggleFeature(FeatureBitset Bits, StringRef Feature,
187                                  ArrayRef<SubtargetFeatureKV> FeatureTable) {
188
189   // Find feature in table.
190   const SubtargetFeatureKV *FeatureEntry =
191       Find(StripFlag(Feature), FeatureTable);
192   // If there is a match
193   if (FeatureEntry) {
194     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
195       Bits &= ~FeatureEntry->Value;
196       // For each feature that implies this, clear it.
197       ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
198     } else {
199       Bits |=  FeatureEntry->Value;
200
201       // For each feature that this implies, set it.
202       SetImpliedBits(Bits, FeatureEntry, FeatureTable);
203     }
204   } else {
205     errs() << "'" << Feature
206            << "' is not a recognized feature for this target"
207            << " (ignoring feature)\n";
208   }
209
210   return Bits;
211 }
212
213
214 /// getFeatureBits - Get feature bits a CPU.
215 ///
216 FeatureBitset
217 SubtargetFeatures::getFeatureBits(StringRef CPU,
218                                   ArrayRef<SubtargetFeatureKV> CPUTable,
219                                   ArrayRef<SubtargetFeatureKV> FeatureTable) {
220
221   if (CPUTable.empty() || FeatureTable.empty())
222     return FeatureBitset();
223
224 #ifndef NDEBUG
225   for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
226     assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
227            "CPU table is not sorted");
228   }
229   for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
230     assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
231           "CPU features table is not sorted");
232   }
233 #endif
234   // Resulting bits
235   FeatureBitset Bits;
236
237   // Check if help is needed
238   if (CPU == "help")
239     Help(CPUTable, FeatureTable);
240
241   // Find CPU entry if CPU name is specified.
242   else if (!CPU.empty()) {
243     const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
244
245     // If there is a match
246     if (CPUEntry) {
247       // Set base feature bits
248       Bits = CPUEntry->Value;
249
250       // Set the feature implied by this CPU feature, if any.
251       for (auto &FE : FeatureTable) {
252         if ((CPUEntry->Value & FE.Value).any())
253           SetImpliedBits(Bits, &FE, FeatureTable);
254       }
255     } else {
256       errs() << "'" << CPU
257              << "' is not a recognized processor for this target"
258              << " (ignoring processor)\n";
259     }
260   }
261
262   // Iterate through each feature
263   for (auto &Feature : Features) {
264     // Check for help
265     if (Feature == "+help")
266       Help(CPUTable, FeatureTable);
267
268     // Find feature in table.
269     const SubtargetFeatureKV *FeatureEntry =
270         Find(StripFlag(Feature), FeatureTable);
271     // If there is a match
272     if (FeatureEntry) {
273       // Enable/disable feature in bits
274       if (isEnabled(Feature)) {
275         Bits |=  FeatureEntry->Value;
276
277         // For each feature that this implies, set it.
278         SetImpliedBits(Bits, FeatureEntry, FeatureTable);
279       } else {
280         Bits &= ~FeatureEntry->Value;
281
282         // For each feature that implies this, clear it.
283         ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
284       }
285     } else {
286       errs() << "'" << Feature
287              << "' is not a recognized feature for this target"
288              << " (ignoring feature)\n";
289     }
290   }
291
292   return Bits;
293 }
294
295 /// print - Print feature string.
296 ///
297 void SubtargetFeatures::print(raw_ostream &OS) const {
298   for (auto &F : Features)
299     OS << F << " ";
300   OS << "\n";
301 }
302
303 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
304 /// dump - Dump feature info.
305 ///
306 void SubtargetFeatures::dump() const {
307   print(dbgs());
308 }
309 #endif
310
311 /// Adds the default features for the specified target triple.
312 ///
313 /// FIXME: This is an inelegant way of specifying the features of a
314 /// subtarget. It would be better if we could encode this information
315 /// into the IR. See <rdar://5972456>.
316 ///
317 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
318   if (Triple.getVendor() == Triple::Apple) {
319     if (Triple.getArch() == Triple::ppc) {
320       // powerpc-apple-*
321       AddFeature("altivec");
322     } else if (Triple.getArch() == Triple::ppc64) {
323       // powerpc64-apple-*
324       AddFeature("64bit");
325       AddFeature("altivec");
326     }
327   }
328 }