Have the SubtargetFeature help routine just not return a number and
[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 /// Split - Splits a string of comma separated items in to a vector of strings.
55 ///
56 static void Split(std::vector<std::string> &V, const StringRef S) {
57   if (S.empty())
58     return;
59
60   // Start at beginning of string.
61   size_t Pos = 0;
62   while (true) {
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));
69       break;
70     }
71     // Otherwise add substring to vector
72     V.push_back(S.substr(Pos, Comma - Pos));
73     // Advance to next item
74     Pos = Comma + 1;
75   }
76 }
77
78 /// Join a vector of strings to a string with a comma separating each element.
79 ///
80 static std::string Join(const std::vector<std::string> &V) {
81   // Start with empty string.
82   std::string Result;
83   // If the vector is not empty
84   if (!V.empty()) {
85     // Start with the first feature
86     Result = V[0];
87     // For each successive feature
88     for (size_t i = 1; i < V.size(); i++) {
89       // Add a comma
90       Result += ",";
91       // Add the feature
92       Result += V[i];
93     }
94   }
95   // Return the features string
96   return Result;
97 }
98
99 /// Adding features.
100 void SubtargetFeatures::AddFeature(const StringRef String) {
101   // Don't add empty features or features we already have.
102   if (!String.empty())
103     // Convert to lowercase, prepend flag if we don't already have a flag.
104     Features.push_back(hasFlag(String) ? String.str() : "+" + String.lower());
105 }
106
107 /// Find KV in array using binary search.
108 static const SubtargetFeatureKV *Find(StringRef S, const SubtargetFeatureKV *A,
109                                       size_t L) {
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
117   return F;
118 }
119
120 /// getLongestEntryLength - Return the length of the longest entry in the table.
121 ///
122 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
123                                     size_t Size) {
124   size_t MaxLen = 0;
125   for (size_t i = 0; i < Size; i++)
126     MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
127   return MaxLen;
128 }
129
130 /// Display help for feature choices.
131 ///
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);
138
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);
144   errs() << '\n';
145
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);
151   errs() << '\n';
152
153   errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
154             "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
155 }
156
157 //===----------------------------------------------------------------------===//
158 //                    SubtargetFeatures Implementation
159 //===----------------------------------------------------------------------===//
160
161 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
162   // Break up string into separate features
163   Split(Features, Initial);
164 }
165
166
167 std::string SubtargetFeatures::getString() const {
168   return Join(Features);
169 }
170
171 /// SetImpliedBits - For each feature that is (transitively) implied by this
172 /// feature, set it.
173 ///
174 static
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];
180
181     if (FeatureEntry->Value == FE.Value) continue;
182
183     if (FeatureEntry->Implies & FE.Value) {
184       Bits |= FE.Value;
185       SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
186     }
187   }
188 }
189
190 /// ClearImpliedBits - For each feature that (transitively) implies this
191 /// feature, clear it.
192 ///
193 static
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];
199
200     if (FeatureEntry->Value == FE.Value) continue;
201
202     if (FE.Implies & FeatureEntry->Value) {
203       Bits &= ~FE.Value;
204       ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
205     }
206   }
207 }
208
209 /// ToggleFeature - Toggle a feature and returns the newly updated feature
210 /// bits.
211 uint64_t
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
219   if (FeatureEntry) {
220     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
221       Bits &= ~FeatureEntry->Value;
222
223       // For each feature that implies this, clear it.
224       ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
225     } else {
226       Bits |=  FeatureEntry->Value;
227
228       // For each feature that this implies, set it.
229       SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
230     }
231   } else {
232     errs() << "'" << Feature
233            << "' is not a recognized feature for this target"
234            << " (ignoring feature)\n";
235   }
236
237   return Bits;
238 }
239
240
241 /// getFeatureBits - Get feature bits a CPU.
242 ///
243 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
244                                          const SubtargetFeatureKV *CPUTable,
245                                          size_t CPUTableSize,
246                                          const SubtargetFeatureKV *FeatureTable,
247                                          size_t FeatureTableSize) {
248   if (!FeatureTableSize || !CPUTableSize)
249     return 0;
250
251 #ifndef NDEBUG
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");
255   }
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");
259   }
260 #endif
261   uint64_t Bits = 0;                    // Resulting bits
262
263   // Check if help is needed
264   if (CPU == "help")
265     Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
266
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
271     if (CPUEntry) {
272       // Set base feature bits
273       Bits = CPUEntry->Value;
274
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);
280       }
281     } else {
282       errs() << "'" << CPU
283              << "' is not a recognized processor for this target"
284              << " (ignoring processor)\n";
285     }
286   }
287
288   // Iterate through each feature
289   for (size_t i = 0, E = Features.size(); i < E; i++) {
290     const StringRef Feature = Features[i];
291
292     // Check for help
293     if (Feature == "+help")
294       Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
295
296     // Find feature in table.
297     const SubtargetFeatureKV *FeatureEntry =
298                        Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
299     // If there is a match
300     if (FeatureEntry) {
301       // Enable/disable feature in bits
302       if (isEnabled(Feature)) {
303         Bits |=  FeatureEntry->Value;
304
305         // For each feature that this implies, set it.
306         SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
307       } else {
308         Bits &= ~FeatureEntry->Value;
309
310         // For each feature that implies this, clear it.
311         ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
312       }
313     } else {
314       errs() << "'" << Feature
315              << "' is not a recognized feature for this target"
316              << " (ignoring feature)\n";
317     }
318   }
319
320   return Bits;
321 }
322
323 /// print - Print feature string.
324 ///
325 void SubtargetFeatures::print(raw_ostream &OS) const {
326   for (size_t i = 0, e = Features.size(); i != e; ++i)
327     OS << Features[i] << "  ";
328   OS << "\n";
329 }
330
331 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
332 /// dump - Dump feature info.
333 ///
334 void SubtargetFeatures::dump() const {
335   print(dbgs());
336 }
337 #endif
338
339 /// Adds the default features for the specified target triple.
340 ///
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>.
344 ///
345 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
346   if (Triple.getVendor() == Triple::Apple) {
347     if (Triple.getArch() == Triple::ppc) {
348       // powerpc-apple-*
349       AddFeature("altivec");
350     } else if (Triple.getArch() == Triple::ppc64) {
351       // powerpc64-apple-*
352       AddFeature("64bit");
353       AddFeature("altivec");
354     }
355   }
356 }