1 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SubtargetFeature interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/SubtargetFeature.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/ADT/StringExtras.h"
22 //===----------------------------------------------------------------------===//
23 // Static Helper Functions
24 //===----------------------------------------------------------------------===//
26 /// hasFlag - Determine if a feature has a flag; '+' or '-'
28 static inline bool hasFlag(const std::string &Feature) {
29 assert(!Feature.empty() && "Empty string");
30 // Get first character
32 // Check if first character is '+' or '-' flag
33 return Ch == '+' || Ch =='-';
36 /// StripFlag - Return string stripped of flag.
38 static inline std::string StripFlag(const std::string &Feature) {
39 return hasFlag(Feature) ? Feature.substr(1) : Feature;
42 /// isEnabled - Return true if enable flag; '+'.
44 static inline bool isEnabled(const std::string &Feature) {
45 assert(!Feature.empty() && "Empty string");
46 // Get first character
48 // Check if first character is '+' for enabled
52 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
54 static inline std::string PrependFlag(const std::string &Feature,
56 assert(!Feature.empty() && "Empty string");
57 if (hasFlag(Feature)) return Feature;
58 return std::string(IsEnabled ? "+" : "-") + Feature;
61 /// Split - Splits a string of comma separated items in to a vector of strings.
63 static void Split(std::vector<std::string> &V, const std::string &S) {
64 // Start at beginning of string.
67 // Find the next comma
68 size_t Comma = S.find(',', Pos);
69 // If no comma found then the the rest of the string is used
70 if (Comma == std::string::npos) {
71 // Add string to vector
72 V.push_back(S.substr(Pos));
75 // Otherwise add substring to vector
76 V.push_back(S.substr(Pos, Comma - Pos));
77 // Advance to next item
82 /// Join a vector of strings to a string with a comma separating each element.
84 static std::string Join(const std::vector<std::string> &V) {
85 // Start with empty string.
87 // If the vector is not empty
89 // Start with the CPU feature
91 // For each successive feature
92 for (size_t i = 1; i < V.size(); i++) {
99 // Return the features string
104 void SubtargetFeatures::AddFeature(const std::string &String,
106 // Don't add empty features
107 if (!String.empty()) {
108 // Convert to lowercase, prepend flag and add to vector
109 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
113 /// Add a set of features from the comma-separated string.
114 void SubtargetFeatures::AddFeatures(const std::string &String)
116 std::vector<std::string> _Features;
118 Split(_Features, String);
119 // Nothing is specified.
120 if (_Features.size() == 0)
123 for (std::vector<std::string>::iterator it = _Features.begin(),
124 end = _Features.end(); it != end; ++it) {
125 // AddFeature will take care of feature string normalization.
130 /// Add a set of features from the parsed command line parameters.
131 void SubtargetFeatures::AddFeatures(const cl::list<std::string> &List)
133 for (cl::list<std::string>::const_iterator it = List.begin(),
134 end = List.end(); it != end; ++it) {
135 // AddFeature will take care of feature string normalization.
140 /// Find KV in array using binary search.
141 template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
142 // Make the lower bound element we're looking for
145 // Determine the end of the array
147 // Binary search the array
148 const T *F = std::lower_bound(A, Hi, KV);
149 // If not found then return NULL
150 if (F == Hi || std::string(F->Key) != S) return NULL;
151 // Return the found array item
155 /// getLongestEntryLength - Return the length of the longest entry in the table.
157 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
160 for (size_t i = 0; i < Size; i++)
161 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
165 /// Display help for feature choices.
167 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
168 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
169 // Determine the length of the longest CPU and Feature entries.
170 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
171 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
173 // Print the CPU table.
174 errs() << "Available CPUs for this target:\n\n";
175 for (size_t i = 0; i != CPUTableSize; i++)
176 errs() << " " << CPUTable[i].Key
177 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
178 << " - " << CPUTable[i].Desc << ".\n";
181 // Print the Feature table.
182 errs() << "Available features for this target:\n\n";
183 for (size_t i = 0; i != FeatTableSize; i++)
184 errs() << " " << FeatTable[i].Key
185 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
186 << " - " << FeatTable[i].Desc << ".\n";
189 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
190 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
194 //===----------------------------------------------------------------------===//
195 // SubtargetFeatures Implementation
196 //===----------------------------------------------------------------------===//
198 SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
199 // Break up string into separate features
200 Split(Features, Initial);
204 std::string SubtargetFeatures::getString() const {
205 return Join(Features);
207 void SubtargetFeatures::setString(const std::string &Initial) {
208 // Throw out old features
210 // Break up string into separate features
211 Split(Features, LowercaseString(Initial));
215 /// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
217 void SubtargetFeatures::setCPU(const std::string &String) {
218 Features[0] = LowercaseString(String);
222 /// setCPUIfNone - Setting CPU string only if no string is set.
224 void SubtargetFeatures::setCPUIfNone(const std::string &String) {
225 if (Features[0].empty()) setCPU(String);
228 /// getCPU - Returns current CPU.
230 const std::string & SubtargetFeatures::getCPU() const {
235 /// SetImpliedBits - For each feature that is (transitively) implied by this
239 void SetImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry,
240 const SubtargetFeatureKV *FeatureTable,
241 size_t FeatureTableSize) {
242 for (size_t i = 0; i < FeatureTableSize; ++i) {
243 const SubtargetFeatureKV &FE = FeatureTable[i];
245 if (FeatureEntry->Value == FE.Value) continue;
247 if (FeatureEntry->Implies & FE.Value) {
249 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
254 /// ClearImpliedBits - For each feature that (transitively) implies this
255 /// feature, clear it.
258 void ClearImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry,
259 const SubtargetFeatureKV *FeatureTable,
260 size_t FeatureTableSize) {
261 for (size_t i = 0; i < FeatureTableSize; ++i) {
262 const SubtargetFeatureKV &FE = FeatureTable[i];
264 if (FeatureEntry->Value == FE.Value) continue;
266 if (FE.Implies & FeatureEntry->Value) {
268 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
273 /// getBits - Get feature bits.
275 uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
277 const SubtargetFeatureKV *FeatureTable,
278 size_t FeatureTableSize) {
279 assert(CPUTable && "missing CPU table");
280 assert(FeatureTable && "missing features table");
282 for (size_t i = 1; i < CPUTableSize; i++) {
283 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
284 "CPU table is not sorted");
286 for (size_t i = 1; i < FeatureTableSize; i++) {
287 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
288 "CPU features table is not sorted");
291 uint32_t Bits = 0; // Resulting bits
293 // Check if help is needed
294 if (Features[0] == "help")
295 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
298 const SubtargetFeatureKV *CPUEntry =
299 Find(Features[0], CPUTable, CPUTableSize);
300 // If there is a match
302 // Set base feature bits
303 Bits = CPUEntry->Value;
305 // Set the feature implied by this CPU feature, if any.
306 for (size_t i = 0; i < FeatureTableSize; ++i) {
307 const SubtargetFeatureKV &FE = FeatureTable[i];
308 if (CPUEntry->Value & FE.Value)
309 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
312 errs() << "'" << Features[0]
313 << "' is not a recognized processor for this target"
314 << " (ignoring processor)\n";
316 // Iterate through each feature
317 for (size_t i = 1; i < Features.size(); i++) {
318 const std::string &Feature = Features[i];
321 if (Feature == "+help")
322 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
324 // Find feature in table.
325 const SubtargetFeatureKV *FeatureEntry =
326 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
327 // If there is a match
329 // Enable/disable feature in bits
330 if (isEnabled(Feature)) {
331 Bits |= FeatureEntry->Value;
333 // For each feature that this implies, set it.
334 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
336 Bits &= ~FeatureEntry->Value;
338 // For each feature that implies this, clear it.
339 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
342 errs() << "'" << Feature
343 << "' is not a recognized feature for this target"
344 << " (ignoring feature)\n";
352 void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table,
354 assert(Table && "missing table");
356 for (size_t i = 1; i < TableSize; i++) {
357 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
362 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
367 errs() << "'" << Features[0]
368 << "' is not a recognized processor for this target"
369 << " (ignoring processor)\n";
374 /// print - Print feature string.
376 void SubtargetFeatures::print(raw_ostream &OS) const {
377 for (size_t i = 0, e = Features.size(); i != e; ++i)
378 OS << Features[i] << " ";
382 /// dump - Dump feature info.
384 void SubtargetFeatures::dump() const {
388 /// getDefaultSubtargetFeatures - Return a string listing
389 /// the features associated with the target triple.
391 /// FIXME: This is an inelegant way of specifying the features of a
392 /// subtarget. It would be better if we could encode this information
393 /// into the IR. See <rdar://5972456>.
395 std::string SubtargetFeatures::getDefaultSubtargetFeatures(
396 const Triple& Triple) {
397 switch (Triple.getVendor()) {
399 switch (Triple.getArch()) {
400 case Triple::ppc: // powerpc-apple-*
401 return std::string("altivec");
402 case Triple::ppc64: // powerpc64-apple-*
403 return std::string("64bit,altivec");
412 return std::string("");