Add missing standard headers. Patch by Joerg Sonnenberger!
[llvm.git] / lib / Target / SubtargetFeature.cpp
blob3cf95b57c5dcba2f14d8699f6db4304e0bd53c23
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.
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"
18 #include <algorithm>
19 #include <cassert>
20 #include <cctype>
21 #include <cstdlib>
22 using namespace llvm;
24 //===----------------------------------------------------------------------===//
25 // Static Helper Functions
26 //===----------------------------------------------------------------------===//
28 /// hasFlag - Determine if a feature has a flag; '+' or '-'
29 ///
30 static inline bool hasFlag(const std::string &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 =='-';
38 /// StripFlag - Return string stripped of flag.
39 ///
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; '+'.
45 ///
46 static inline bool isEnabled(const std::string &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 == '+';
54 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
55 ///
56 static inline std::string PrependFlag(const std::string &Feature,
57 bool IsEnabled) {
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.
64 ///
65 static void Split(std::vector<std::string> &V, const std::string &S) {
66 // Start at beginning of string.
67 size_t Pos = 0;
68 while (true) {
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));
75 break;
77 // Otherwise add substring to vector
78 V.push_back(S.substr(Pos, Comma - Pos));
79 // Advance to next item
80 Pos = Comma + 1;
84 /// Join a vector of strings to a string with a comma separating each element.
85 ///
86 static std::string Join(const std::vector<std::string> &V) {
87 // Start with empty string.
88 std::string Result;
89 // If the vector is not empty
90 if (!V.empty()) {
91 // Start with the CPU feature
92 Result = V[0];
93 // For each successive feature
94 for (size_t i = 1; i < V.size(); i++) {
95 // Add a comma
96 Result += ",";
97 // Add the feature
98 Result += V[i];
101 // Return the features string
102 return Result;
105 /// Adding features.
106 void SubtargetFeatures::AddFeature(const std::string &String,
107 bool IsEnabled) {
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
118 T KV;
119 KV.Key = S.c_str();
120 // Determine the end of the array
121 const T *Hi = A + L;
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
127 return F;
130 /// getLongestEntryLength - Return the length of the longest entry in the table.
132 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
133 size_t Size) {
134 size_t MaxLen = 0;
135 for (size_t i = 0; i < Size; i++)
136 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
137 return MaxLen;
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";
154 errs() << "\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";
162 errs() << "\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";
166 std::exit(1);
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
184 Features.clear();
185 // Break up string into separate features
186 Split(Features, LowercaseString(Initial));
190 /// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
191 /// clears CPU.
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 {
206 return Features[0];
210 /// SetImpliedBits - For each feature that is (transitively) implied by this
211 /// feature, set it.
213 static
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) {
223 Bits |= FE.Value;
224 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
229 /// ClearImpliedBits - For each feature that (transitively) implies this
230 /// feature, clear it.
231 ///
232 static
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) {
242 Bits &= ~FE.Value;
243 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
248 /// getBits - Get feature bits.
250 uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
251 size_t CPUTableSize,
252 const SubtargetFeatureKV *FeatureTable,
253 size_t FeatureTableSize) {
254 assert(CPUTable && "missing CPU table");
255 assert(FeatureTable && "missing features table");
256 #ifndef NDEBUG
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");
265 #endif
266 uint32_t Bits = 0; // Resulting bits
268 // Check if help is needed
269 if (Features[0] == "help")
270 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
272 // Find CPU entry
273 const SubtargetFeatureKV *CPUEntry =
274 Find(Features[0], CPUTable, CPUTableSize);
275 // If there is a match
276 if (CPUEntry) {
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);
286 } else {
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];
295 // Check for help
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
303 if (FeatureEntry) {
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);
310 } else {
311 Bits &= ~FeatureEntry->Value;
313 // For each feature that implies this, clear it.
314 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
316 } else {
317 errs() << "'" << Feature
318 << "' is not a recognized feature for this target"
319 << " (ignoring feature)\n";
323 return Bits;
326 /// Get info pointer
327 void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table,
328 size_t TableSize) {
329 assert(Table && "missing table");
330 #ifndef NDEBUG
331 for (size_t i = 1; i < TableSize; i++) {
332 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
334 #endif
336 // Find entry
337 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
339 if (Entry) {
340 return Entry->Value;
341 } else {
342 errs() << "'" << Features[0]
343 << "' is not a recognized processor for this target"
344 << " (ignoring processor)\n";
345 return NULL;
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] << " ";
354 OS << "\n";
357 /// dump - Dump feature info.
359 void SubtargetFeatures::dump() const {
360 print(dbgs());
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) {
372 setCPU(CPU);
374 if (Triple.getVendor() == Triple::Apple) {
375 if (Triple.getArch() == Triple::ppc) {
376 // powerpc-apple-*
377 AddFeature("altivec");
378 } else if (Triple.getArch() == Triple::ppc64) {
379 // powerpc64-apple-*
380 AddFeature("64bit");
381 AddFeature("altivec");