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/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/ADT/StringExtras.h"
24 //===----------------------------------------------------------------------===//
25 // Static Helper Functions
26 //===----------------------------------------------------------------------===//
28 /// hasFlag - Determine if a feature has a flag; '+' or '-'
30 static inline bool hasFlag(const std::string &Feature) {
31 assert(!Feature.empty() && "Empty string");
32 // Get first character
34 // Check if first character is '+' or '-' flag
35 return Ch == '+' || Ch =='-';
38 /// StripFlag - Return string stripped of flag.
40 static inline std::string StripFlag(const std::string &Feature) {
41 return hasFlag(Feature) ? Feature.substr(1) : Feature;
44 /// isEnabled - Return true if enable flag; '+'.
46 static inline bool isEnabled(const std::string &Feature) {
47 assert(!Feature.empty() && "Empty string");
48 // Get first character
50 // Check if first character is '+' for enabled
54 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
56 static inline std::string PrependFlag(const std::string &Feature,
58 assert(!Feature.empty() && "Empty string");
59 if (hasFlag(Feature)) return Feature;
60 return std::string(IsEnabled ? "+" : "-") + Feature;
63 /// Split - Splits a string of comma separated items in to a vector of strings.
65 static void Split(std::vector<std::string> &V, const std::string &S) {
66 // Start at beginning of string.
69 // Find the next comma
70 size_t Comma = S.find(',', Pos);
71 // If no comma found then the rest of the string is used
72 if (Comma == std::string::npos) {
73 // Add string to vector
74 V.push_back(S.substr(Pos));
77 // Otherwise add substring to vector
78 V.push_back(S.substr(Pos, Comma - Pos));
79 // Advance to next item
84 /// Join a vector of strings to a string with a comma separating each element.
86 static std::string Join(const std::vector<std::string> &V) {
87 // Start with empty string.
89 // If the vector is not empty
91 // Start with the CPU feature
93 // For each successive feature
94 for (size_t i = 1; i < V.size(); i++) {
101 // Return the features string
106 void SubtargetFeatures::AddFeature(const std::string &String,
108 // Don't add empty features
109 if (!String.empty()) {
110 // Convert to lowercase, prepend flag and add to vector
111 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
115 /// Find KV in array using binary search.
116 template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
117 // Make the lower bound element we're looking for
120 // Determine the end of the array
122 // Binary search the array
123 const T *F = std::lower_bound(A, Hi, KV);
124 // If not found then return NULL
125 if (F == Hi || std::string(F->Key) != S) return NULL;
126 // Return the found array item
130 /// getLongestEntryLength - Return the length of the longest entry in the table.
132 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
135 for (size_t i = 0; i < Size; i++)
136 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
140 /// Display help for feature choices.
142 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
143 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
144 // Determine the length of the longest CPU and Feature entries.
145 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
146 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
148 // Print the CPU table.
149 errs() << "Available CPUs for this target:\n\n";
150 for (size_t i = 0; i != CPUTableSize; i++)
151 errs() << " " << CPUTable[i].Key
152 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
153 << " - " << CPUTable[i].Desc << ".\n";
156 // Print the Feature table.
157 errs() << "Available features for this target:\n\n";
158 for (size_t i = 0; i != FeatTableSize; i++)
159 errs() << " " << FeatTable[i].Key
160 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
161 << " - " << FeatTable[i].Desc << ".\n";
164 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
165 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
169 //===----------------------------------------------------------------------===//
170 // SubtargetFeatures Implementation
171 //===----------------------------------------------------------------------===//
173 SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
174 // Break up string into separate features
175 Split(Features, Initial);
179 std::string SubtargetFeatures::getString() const {
180 return Join(Features);
182 void SubtargetFeatures::setString(const std::string &Initial) {
183 // Throw out old features
185 // Break up string into separate features
186 Split(Features, LowercaseString(Initial));
190 /// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
192 void SubtargetFeatures::setCPU(const std::string &String) {
193 Features[0] = LowercaseString(String);
197 /// setCPUIfNone - Setting CPU string only if no string is set.
199 void SubtargetFeatures::setCPUIfNone(const std::string &String) {
200 if (Features[0].empty()) setCPU(String);
203 /// getCPU - Returns current CPU.
205 const std::string & SubtargetFeatures::getCPU() const {
210 /// SetImpliedBits - For each feature that is (transitively) implied by this
214 void SetImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry,
215 const SubtargetFeatureKV *FeatureTable,
216 size_t FeatureTableSize) {
217 for (size_t i = 0; i < FeatureTableSize; ++i) {
218 const SubtargetFeatureKV &FE = FeatureTable[i];
220 if (FeatureEntry->Value == FE.Value) continue;
222 if (FeatureEntry->Implies & FE.Value) {
224 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
229 /// ClearImpliedBits - For each feature that (transitively) implies this
230 /// feature, clear it.
233 void ClearImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry,
234 const SubtargetFeatureKV *FeatureTable,
235 size_t FeatureTableSize) {
236 for (size_t i = 0; i < FeatureTableSize; ++i) {
237 const SubtargetFeatureKV &FE = FeatureTable[i];
239 if (FeatureEntry->Value == FE.Value) continue;
241 if (FE.Implies & FeatureEntry->Value) {
243 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
248 /// getBits - Get feature bits.
250 uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
252 const SubtargetFeatureKV *FeatureTable,
253 size_t FeatureTableSize) {
254 assert(CPUTable && "missing CPU table");
255 assert(FeatureTable && "missing features table");
257 for (size_t i = 1; i < CPUTableSize; i++) {
258 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
259 "CPU table is not sorted");
261 for (size_t i = 1; i < FeatureTableSize; i++) {
262 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
263 "CPU features table is not sorted");
266 uint32_t Bits = 0; // Resulting bits
268 // Check if help is needed
269 if (Features[0] == "help")
270 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
273 const SubtargetFeatureKV *CPUEntry =
274 Find(Features[0], CPUTable, CPUTableSize);
275 // If there is a match
277 // Set base feature bits
278 Bits = CPUEntry->Value;
280 // Set the feature implied by this CPU feature, if any.
281 for (size_t i = 0; i < FeatureTableSize; ++i) {
282 const SubtargetFeatureKV &FE = FeatureTable[i];
283 if (CPUEntry->Value & FE.Value)
284 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
287 errs() << "'" << Features[0]
288 << "' is not a recognized processor for this target"
289 << " (ignoring processor)\n";
291 // Iterate through each feature
292 for (size_t i = 1; i < Features.size(); i++) {
293 const std::string &Feature = Features[i];
296 if (Feature == "+help")
297 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
299 // Find feature in table.
300 const SubtargetFeatureKV *FeatureEntry =
301 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
302 // If there is a match
304 // Enable/disable feature in bits
305 if (isEnabled(Feature)) {
306 Bits |= FeatureEntry->Value;
308 // For each feature that this implies, set it.
309 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
311 Bits &= ~FeatureEntry->Value;
313 // For each feature that implies this, clear it.
314 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
317 errs() << "'" << Feature
318 << "' is not a recognized feature for this target"
319 << " (ignoring feature)\n";
327 void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table,
329 assert(Table && "missing table");
331 for (size_t i = 1; i < TableSize; i++) {
332 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
337 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
342 errs() << "'" << Features[0]
343 << "' is not a recognized processor for this target"
344 << " (ignoring processor)\n";
349 /// print - Print feature string.
351 void SubtargetFeatures::print(raw_ostream &OS) const {
352 for (size_t i = 0, e = Features.size(); i != e; ++i)
353 OS << Features[i] << " ";
357 /// dump - Dump feature info.
359 void SubtargetFeatures::dump() const {
363 /// getDefaultSubtargetFeatures - Return a string listing the features
364 /// associated with the target triple.
366 /// FIXME: This is an inelegant way of specifying the features of a
367 /// subtarget. It would be better if we could encode this information
368 /// into the IR. See <rdar://5972456>.
370 void SubtargetFeatures::getDefaultSubtargetFeatures(const std::string &CPU,
371 const Triple& Triple) {
374 if (Triple.getVendor() == Triple::Apple) {
375 if (Triple.getArch() == Triple::ppc) {
377 AddFeature("altivec");
378 } else if (Triple.getArch() == Triple::ppc64) {
381 AddFeature("altivec");