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