• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ///
hasFlag(const StringRef Feature)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 ///
StripFlag(const StringRef Feature)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 ///
isEnabled(const StringRef Feature)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 ///
Split(std::vector<std::string> & V,const StringRef S)56 static void Split(std::vector<std::string> &V, const StringRef S) {
57   SmallVector<StringRef, 2> 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 ///
Join(const std::vector<std::string> & V)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.
AddFeature(const StringRef String)84 void SubtargetFeatures::AddFeature(const 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.
Find(StringRef S,ArrayRef<SubtargetFeatureKV> A)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 ///
getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table)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 ///
Help(ArrayRef<SubtargetFeatureKV> CPUTable,ArrayRef<SubtargetFeatureKV> FeatTable)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 
SubtargetFeatures(const StringRef Initial)139 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
140   // Break up string into separate features
141   Split(Features, Initial);
142 }
143 
144 
getString() const145 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
SetImpliedBits(uint64_t & Bits,const SubtargetFeatureKV * FeatureEntry,ArrayRef<SubtargetFeatureKV> FeatureTable)153 void SetImpliedBits(uint64_t &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) {
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
ClearImpliedBits(uint64_t & Bits,const SubtargetFeatureKV * FeatureEntry,ArrayRef<SubtargetFeatureKV> FeatureTable)169 void ClearImpliedBits(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 (FE.Implies & FeatureEntry->Value) {
175       Bits &= ~FE.Value;
176       ClearImpliedBits(Bits, &FE, FeatureTable);
177     }
178   }
179 }
180 
181 /// ToggleFeature - Toggle a feature and returns the newly updated feature
182 /// bits.
183 uint64_t
ToggleFeature(uint64_t Bits,const StringRef Feature,ArrayRef<SubtargetFeatureKV> FeatureTable)184 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
185                                  ArrayRef<SubtargetFeatureKV> FeatureTable) {
186 
187   // Find feature in table.
188   const SubtargetFeatureKV *FeatureEntry =
189       Find(StripFlag(Feature), FeatureTable);
190   // If there is a match
191   if (FeatureEntry) {
192     if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
193       Bits &= ~FeatureEntry->Value;
194 
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 uint64_t
getFeatureBits(const StringRef CPU,ArrayRef<SubtargetFeatureKV> CPUTable,ArrayRef<SubtargetFeatureKV> FeatureTable)216 SubtargetFeatures::getFeatureBits(const StringRef CPU,
217                                   ArrayRef<SubtargetFeatureKV> CPUTable,
218                                   ArrayRef<SubtargetFeatureKV> FeatureTable) {
219 
220   if (CPUTable.empty() || FeatureTable.empty())
221     return 0;
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   uint64_t Bits = 0;                    // Resulting bits
234 
235   // Check if help is needed
236   if (CPU == "help")
237     Help(CPUTable, FeatureTable);
238 
239   // Find CPU entry if CPU name is specified.
240   else if (!CPU.empty()) {
241     const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
242 
243     // If there is a match
244     if (CPUEntry) {
245       // Set base feature bits
246       Bits = CPUEntry->Value;
247 
248       // Set the feature implied by this CPU feature, if any.
249       for (auto &FE : FeatureTable) {
250         if (CPUEntry->Value & FE.Value)
251           SetImpliedBits(Bits, &FE, FeatureTable);
252       }
253     } else {
254       errs() << "'" << CPU
255              << "' is not a recognized processor for this target"
256              << " (ignoring processor)\n";
257     }
258   }
259 
260   // Iterate through each feature
261   for (auto &Feature : Features) {
262     // Check for help
263     if (Feature == "+help")
264       Help(CPUTable, FeatureTable);
265 
266     // Find feature in table.
267     const SubtargetFeatureKV *FeatureEntry =
268         Find(StripFlag(Feature), FeatureTable);
269     // If there is a match
270     if (FeatureEntry) {
271       // Enable/disable feature in bits
272       if (isEnabled(Feature)) {
273         Bits |=  FeatureEntry->Value;
274 
275         // For each feature that this implies, set it.
276         SetImpliedBits(Bits, FeatureEntry, FeatureTable);
277       } else {
278         Bits &= ~FeatureEntry->Value;
279 
280         // For each feature that implies this, clear it.
281         ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
282       }
283     } else {
284       errs() << "'" << Feature
285              << "' is not a recognized feature for this target"
286              << " (ignoring feature)\n";
287     }
288   }
289 
290   return Bits;
291 }
292 
293 /// print - Print feature string.
294 ///
print(raw_ostream & OS) const295 void SubtargetFeatures::print(raw_ostream &OS) const {
296   for (auto &F : Features)
297     OS << F << " ";
298   OS << "\n";
299 }
300 
301 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
302 /// dump - Dump feature info.
303 ///
dump() const304 void SubtargetFeatures::dump() const {
305   print(dbgs());
306 }
307 #endif
308 
309 /// Adds the default features for the specified target triple.
310 ///
311 /// FIXME: This is an inelegant way of specifying the features of a
312 /// subtarget. It would be better if we could encode this information
313 /// into the IR. See <rdar://5972456>.
314 ///
getDefaultSubtargetFeatures(const Triple & Triple)315 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
316   if (Triple.getVendor() == Triple::Apple) {
317     if (Triple.getArch() == Triple::ppc) {
318       // powerpc-apple-*
319       AddFeature("altivec");
320     } else if (Triple.getArch() == Triple::ppc64) {
321       // powerpc64-apple-*
322       AddFeature("64bit");
323       AddFeature("altivec");
324     }
325   }
326 }
327