Fix odd formatting that snuck into last patch.
[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,
109                                       ArrayRef<SubtargetFeatureKV> A) {
110   // Binary search the array
111   auto F = std::lower_bound(A.begin(), A.end(), S);
112   // If not found then return NULL
113   if (F == A.end() || StringRef(F->Key) != S) return nullptr;
114   // Return the found array item
115   return F;
116 }
117
118 /// getLongestEntryLength - Return the length of the longest entry in the table.
119 ///
120 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
121   size_t MaxLen = 0;
122   for (auto &I : Table)
123     MaxLen = std::max(MaxLen, std::strlen(I.Key));
124   return MaxLen;
125 }
126
127 /// Display help for feature choices.
128 ///
129 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
130                  ArrayRef<SubtargetFeatureKV> FeatTable) {
131   // Determine the length of the longest CPU and Feature entries.
132   unsigned MaxCPULen  = getLongestEntryLength(CPUTable);
133   unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
134
135   // Print the CPU table.
136   errs() << "Available CPUs for this target:\n\n";
137   for (auto &CPU : CPUTable)
138     errs() << format("  %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
139   errs() << '\n';
140
141   // Print the Feature table.
142   errs() << "Available features for this target:\n\n";
143   for (auto &Feature : FeatTable)
144     errs() << format("  %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
145   errs() << '\n';
146
147   errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
148             "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
149 }
150
151 //===----------------------------------------------------------------------===//
152 //                    SubtargetFeatures Implementation
153 //===----------------------------------------------------------------------===//
154
155 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
156   // Break up string into separate features
157   Split(Features, Initial);
158 }
159
160
161 std::string SubtargetFeatures::getString() const {
162   return Join(Features);
163 }
164
165 /// SetImpliedBits - For each feature that is (transitively) implied by this
166 /// feature, set it.
167 ///
168 static
169 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
170                     ArrayRef<SubtargetFeatureKV> FeatureTable) {
171   for (auto &FE : FeatureTable) {
172     if (FeatureEntry->Value == FE.Value) continue;
173
174     if (FeatureEntry->Implies & FE.Value) {
175       Bits |= FE.Value;
176       SetImpliedBits(Bits, &FE, FeatureTable);
177     }
178   }
179 }
180
181 /// ClearImpliedBits - For each feature that (transitively) implies this
182 /// feature, clear it.
183 ///
184 static
185 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
186                       ArrayRef<SubtargetFeatureKV> FeatureTable) {
187   for (auto &FE : FeatureTable) {
188     if (FeatureEntry->Value == FE.Value) continue;
189
190     if (FE.Implies & FeatureEntry->Value) {
191       Bits &= ~FE.Value;
192       ClearImpliedBits(Bits, &FE, FeatureTable);
193     }
194   }
195 }
196
197 /// ToggleFeature - Toggle a feature and returns the newly updated feature
198 /// bits.
199 uint64_t
200 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
201                                  ArrayRef<SubtargetFeatureKV> FeatureTable) {
202
203   // Find feature in table.
204   const SubtargetFeatureKV *FeatureEntry =
205       Find(StripFlag(Feature), FeatureTable);
206   // If there is a match
207   if (FeatureEntry) {
208     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
209       Bits &= ~FeatureEntry->Value;
210
211       // For each feature that implies this, clear it.
212       ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
213     } else {
214       Bits |=  FeatureEntry->Value;
215
216       // For each feature that this implies, set it.
217       SetImpliedBits(Bits, FeatureEntry, FeatureTable);
218     }
219   } else {
220     errs() << "'" << Feature
221            << "' is not a recognized feature for this target"
222            << " (ignoring feature)\n";
223   }
224
225   return Bits;
226 }
227
228
229 /// getFeatureBits - Get feature bits a CPU.
230 ///
231 uint64_t
232 SubtargetFeatures::getFeatureBits(const StringRef CPU,
233                                   ArrayRef<SubtargetFeatureKV> CPUTable,
234                                   ArrayRef<SubtargetFeatureKV> FeatureTable) {
235
236   if (CPUTable.empty() || FeatureTable.empty())
237     return 0;
238
239 #ifndef NDEBUG
240   for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
241     assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
242            "CPU table is not sorted");
243   }
244   for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
245     assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
246           "CPU features table is not sorted");
247   }
248 #endif
249   uint64_t Bits = 0;                    // Resulting bits
250
251   // Check if help is needed
252   if (CPU == "help")
253     Help(CPUTable, FeatureTable);
254
255   // Find CPU entry if CPU name is specified.
256   else if (!CPU.empty()) {
257     const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
258
259     // If there is a match
260     if (CPUEntry) {
261       // Set base feature bits
262       Bits = CPUEntry->Value;
263
264       // Set the feature implied by this CPU feature, if any.
265       for (auto &FE : FeatureTable) {
266         if (CPUEntry->Value & FE.Value)
267           SetImpliedBits(Bits, &FE, FeatureTable);
268       }
269     } else {
270       errs() << "'" << CPU
271              << "' is not a recognized processor for this target"
272              << " (ignoring processor)\n";
273     }
274   }
275
276   // Iterate through each feature
277   for (auto &Feature : Features) {
278     // Check for help
279     if (Feature == "+help")
280       Help(CPUTable, FeatureTable);
281
282     // Find feature in table.
283     const SubtargetFeatureKV *FeatureEntry =
284         Find(StripFlag(Feature), FeatureTable);
285     // If there is a match
286     if (FeatureEntry) {
287       // Enable/disable feature in bits
288       if (isEnabled(Feature)) {
289         Bits |=  FeatureEntry->Value;
290
291         // For each feature that this implies, set it.
292         SetImpliedBits(Bits, FeatureEntry, FeatureTable);
293       } else {
294         Bits &= ~FeatureEntry->Value;
295
296         // For each feature that implies this, clear it.
297         ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
298       }
299     } else {
300       errs() << "'" << Feature
301              << "' is not a recognized feature for this target"
302              << " (ignoring feature)\n";
303     }
304   }
305
306   return Bits;
307 }
308
309 /// print - Print feature string.
310 ///
311 void SubtargetFeatures::print(raw_ostream &OS) const {
312   for (size_t i = 0, e = Features.size(); i != e; ++i)
313     OS << Features[i] << "  ";
314   OS << "\n";
315 }
316
317 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
318 /// dump - Dump feature info.
319 ///
320 void SubtargetFeatures::dump() const {
321   print(dbgs());
322 }
323 #endif
324
325 /// Adds the default features for the specified target triple.
326 ///
327 /// FIXME: This is an inelegant way of specifying the features of a
328 /// subtarget. It would be better if we could encode this information
329 /// into the IR. See <rdar://5972456>.
330 ///
331 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
332   if (Triple.getVendor() == Triple::Apple) {
333     if (Triple.getArch() == Triple::ppc) {
334       // powerpc-apple-*
335       AddFeature("altivec");
336     } else if (Triple.getArch() == Triple::ppc64) {
337       // powerpc64-apple-*
338       AddFeature("64bit");
339       AddFeature("altivec");
340     }
341   }
342 }