Remove unused argument from AddFeature.
[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(const 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(const 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(const 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 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
55 ///
56 static inline std::string PrependFlag(const StringRef Feature,
57                                     bool IsEnabled) {
58   assert(!Feature.empty() && "Empty string");
59   if (hasFlag(Feature))
60     return Feature;
61   std::string Prefix = IsEnabled ? "+" : "-";
62   Prefix += Feature;
63   return Prefix;
64 }
65
66 /// Split - Splits a string of comma separated items in to a vector of strings.
67 ///
68 static void Split(std::vector<std::string> &V, const StringRef S) {
69   if (S.empty())
70     return;
71
72   // Start at beginning of string.
73   size_t Pos = 0;
74   while (true) {
75     // Find the next comma
76     size_t Comma = S.find(',', Pos);
77     // If no comma found then the rest of the string is used
78     if (Comma == std::string::npos) {
79       // Add string to vector
80       V.push_back(S.substr(Pos));
81       break;
82     }
83     // Otherwise add substring to vector
84     V.push_back(S.substr(Pos, Comma - Pos));
85     // Advance to next item
86     Pos = Comma + 1;
87   }
88 }
89
90 /// Join a vector of strings to a string with a comma separating each element.
91 ///
92 static std::string Join(const std::vector<std::string> &V) {
93   // Start with empty string.
94   std::string Result;
95   // If the vector is not empty
96   if (!V.empty()) {
97     // Start with the first feature
98     Result = V[0];
99     // For each successive feature
100     for (size_t i = 1; i < V.size(); i++) {
101       // Add a comma
102       Result += ",";
103       // Add the feature
104       Result += V[i];
105     }
106   }
107   // Return the features string
108   return Result;
109 }
110
111 /// Adding features.
112 void SubtargetFeatures::AddFeature(const StringRef String) {
113   // Don't add empty features
114   if (!String.empty()) {
115     // Convert to lowercase, prepend flag and add to vector
116     Features.push_back(PrependFlag(String.lower(), true));
117   }
118 }
119
120 /// Find KV in array using binary search.
121 static const SubtargetFeatureKV *Find(StringRef S, const SubtargetFeatureKV *A,
122                                       size_t L) {
123   // Determine the end of the array
124   const SubtargetFeatureKV *Hi = A + L;
125   // Binary search the array
126   const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S);
127   // If not found then return NULL
128   if (F == Hi || StringRef(F->Key) != S) return nullptr;
129   // Return the found array item
130   return F;
131 }
132
133 /// getLongestEntryLength - Return the length of the longest entry in the table.
134 ///
135 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
136                                     size_t Size) {
137   size_t MaxLen = 0;
138   for (size_t i = 0; i < Size; i++)
139     MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
140   return MaxLen;
141 }
142
143 /// Display help for feature choices.
144 ///
145 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
146                  const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
147   // Determine the length of the longest CPU and Feature entries.
148   unsigned MaxCPULen  = getLongestEntryLength(CPUTable, CPUTableSize);
149   unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
150
151   // Print the CPU table.
152   errs() << "Available CPUs for this target:\n\n";
153   for (size_t i = 0; i != CPUTableSize; i++)
154     errs() << format("  %-*s - %s.\n",
155                      MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
156   errs() << '\n';
157
158   // Print the Feature table.
159   errs() << "Available features for this target:\n\n";
160   for (size_t i = 0; i != FeatTableSize; i++)
161     errs() << format("  %-*s - %s.\n",
162                      MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
163   errs() << '\n';
164
165   errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
166             "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
167   std::exit(1);
168 }
169
170 //===----------------------------------------------------------------------===//
171 //                    SubtargetFeatures Implementation
172 //===----------------------------------------------------------------------===//
173
174 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
175   // Break up string into separate features
176   Split(Features, Initial);
177 }
178
179
180 std::string SubtargetFeatures::getString() const {
181   return Join(Features);
182 }
183
184 /// SetImpliedBits - For each feature that is (transitively) implied by this
185 /// feature, set it.
186 ///
187 static
188 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
189                     const SubtargetFeatureKV *FeatureTable,
190                     size_t FeatureTableSize) {
191   for (size_t i = 0; i < FeatureTableSize; ++i) {
192     const SubtargetFeatureKV &FE = FeatureTable[i];
193
194     if (FeatureEntry->Value == FE.Value) continue;
195
196     if (FeatureEntry->Implies & FE.Value) {
197       Bits |= FE.Value;
198       SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
199     }
200   }
201 }
202
203 /// ClearImpliedBits - For each feature that (transitively) implies this
204 /// feature, clear it.
205 ///
206 static
207 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
208                       const SubtargetFeatureKV *FeatureTable,
209                       size_t FeatureTableSize) {
210   for (size_t i = 0; i < FeatureTableSize; ++i) {
211     const SubtargetFeatureKV &FE = FeatureTable[i];
212
213     if (FeatureEntry->Value == FE.Value) continue;
214
215     if (FE.Implies & FeatureEntry->Value) {
216       Bits &= ~FE.Value;
217       ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
218     }
219   }
220 }
221
222 /// ToggleFeature - Toggle a feature and returns the newly updated feature
223 /// bits.
224 uint64_t
225 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
226                                  const SubtargetFeatureKV *FeatureTable,
227                                  size_t FeatureTableSize) {
228   // Find feature in table.
229   const SubtargetFeatureKV *FeatureEntry =
230     Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
231   // If there is a match
232   if (FeatureEntry) {
233     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
234       Bits &= ~FeatureEntry->Value;
235
236       // For each feature that implies this, clear it.
237       ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
238     } else {
239       Bits |=  FeatureEntry->Value;
240
241       // For each feature that this implies, set it.
242       SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
243     }
244   } else {
245     errs() << "'" << Feature
246            << "' is not a recognized feature for this target"
247            << " (ignoring feature)\n";
248   }
249
250   return Bits;
251 }
252
253
254 /// getFeatureBits - Get feature bits a CPU.
255 ///
256 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
257                                          const SubtargetFeatureKV *CPUTable,
258                                          size_t CPUTableSize,
259                                          const SubtargetFeatureKV *FeatureTable,
260                                          size_t FeatureTableSize) {
261   if (!FeatureTableSize || !CPUTableSize)
262     return 0;
263
264 #ifndef NDEBUG
265   for (size_t i = 1; i < CPUTableSize; i++) {
266     assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
267            "CPU table is not sorted");
268   }
269   for (size_t i = 1; i < FeatureTableSize; i++) {
270     assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
271           "CPU features table is not sorted");
272   }
273 #endif
274   uint64_t Bits = 0;                    // Resulting bits
275
276   // Check if help is needed
277   if (CPU == "help")
278     Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
279
280   // Find CPU entry if CPU name is specified.
281   if (!CPU.empty()) {
282     const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
283     // If there is a match
284     if (CPUEntry) {
285       // Set base feature bits
286       Bits = CPUEntry->Value;
287
288       // Set the feature implied by this CPU feature, if any.
289       for (size_t i = 0; i < FeatureTableSize; ++i) {
290         const SubtargetFeatureKV &FE = FeatureTable[i];
291         if (CPUEntry->Value & FE.Value)
292           SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
293       }
294     } else {
295       errs() << "'" << CPU
296              << "' is not a recognized processor for this target"
297              << " (ignoring processor)\n";
298     }
299   }
300
301   // Iterate through each feature
302   for (size_t i = 0, E = Features.size(); i < E; i++) {
303     const StringRef Feature = Features[i];
304
305     // Check for help
306     if (Feature == "+help")
307       Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
308
309     // Find feature in table.
310     const SubtargetFeatureKV *FeatureEntry =
311                        Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
312     // If there is a match
313     if (FeatureEntry) {
314       // Enable/disable feature in bits
315       if (isEnabled(Feature)) {
316         Bits |=  FeatureEntry->Value;
317
318         // For each feature that this implies, set it.
319         SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
320       } else {
321         Bits &= ~FeatureEntry->Value;
322
323         // For each feature that implies this, clear it.
324         ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
325       }
326     } else {
327       errs() << "'" << Feature
328              << "' is not a recognized feature for this target"
329              << " (ignoring feature)\n";
330     }
331   }
332
333   return Bits;
334 }
335
336 /// print - Print feature string.
337 ///
338 void SubtargetFeatures::print(raw_ostream &OS) const {
339   for (size_t i = 0, e = Features.size(); i != e; ++i)
340     OS << Features[i] << "  ";
341   OS << "\n";
342 }
343
344 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
345 /// dump - Dump feature info.
346 ///
347 void SubtargetFeatures::dump() const {
348   print(dbgs());
349 }
350 #endif
351
352 /// Adds the default features for the specified target triple.
353 ///
354 /// FIXME: This is an inelegant way of specifying the features of a
355 /// subtarget. It would be better if we could encode this information
356 /// into the IR. See <rdar://5972456>.
357 ///
358 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
359   if (Triple.getVendor() == Triple::Apple) {
360     if (Triple.getArch() == Triple::ppc) {
361       // powerpc-apple-*
362       AddFeature("altivec");
363     } else if (Triple.getArch() == Triple::ppc64) {
364       // powerpc64-apple-*
365       AddFeature("64bit");
366       AddFeature("altivec");
367     }
368   }
369 }