blob: 1085668a76250686c3b80b5bcad53c362d2809e9 [file] [log] [blame]
Jim Laskeyb3302db2005-09-01 21:36:18 +00001//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeyb3302db2005-09-01 21:36:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SubtargetFeature interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/SubtargetFeature.h"
Chris Lattnere0c86af2009-08-23 21:41:43 +000015#include "llvm/Support/raw_ostream.h"
Chris Lattner54195c12005-10-23 05:26:26 +000016#include "llvm/ADT/StringExtras.h"
Jim Laskeyb3302db2005-09-01 21:36:18 +000017#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000018#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000019#include <cctype>
Jim Laskeyb3302db2005-09-01 21:36:18 +000020using namespace llvm;
21
Chris Lattner54195c12005-10-23 05:26:26 +000022//===----------------------------------------------------------------------===//
23// Static Helper Functions
24//===----------------------------------------------------------------------===//
25
26/// hasFlag - Determine if a feature has a flag; '+' or '-'
27///
28static inline bool hasFlag(const std::string &Feature) {
29 assert(!Feature.empty() && "Empty string");
30 // Get first character
31 char Ch = Feature[0];
32 // Check if first character is '+' or '-' flag
33 return Ch == '+' || Ch =='-';
34}
35
36/// StripFlag - Return string stripped of flag.
37///
38static inline std::string StripFlag(const std::string &Feature) {
39 return hasFlag(Feature) ? Feature.substr(1) : Feature;
40}
41
42/// isEnabled - Return true if enable flag; '+'.
43///
44static inline bool isEnabled(const std::string &Feature) {
45 assert(!Feature.empty() && "Empty string");
46 // Get first character
47 char Ch = Feature[0];
48 // Check if first character is '+' for enabled
49 return Ch == '+';
50}
51
52/// PrependFlag - Return a string with a prepended flag; '+' or '-'.
53///
54static inline std::string PrependFlag(const std::string &Feature,
55 bool IsEnabled) {
56 assert(!Feature.empty() && "Empty string");
57 if (hasFlag(Feature)) return Feature;
58 return std::string(IsEnabled ? "+" : "-") + Feature;
59}
60
61/// Split - Splits a string of comma separated items in to a vector of strings.
62///
63static void Split(std::vector<std::string> &V, const std::string &S) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000064 // Start at beginning of string.
65 size_t Pos = 0;
66 while (true) {
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));
73 break;
74 }
75 // Otherwise add substring to vector
76 V.push_back(S.substr(Pos, Comma - Pos));
77 // Advance to next item
78 Pos = Comma + 1;
79 }
80}
81
82/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner54195c12005-10-23 05:26:26 +000083///
84static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000085 // Start with empty string.
86 std::string Result;
87 // If the vector is not empty
88 if (!V.empty()) {
89 // Start with the CPU feature
90 Result = V[0];
91 // For each successive feature
92 for (size_t i = 1; i < V.size(); i++) {
93 // Add a comma
94 Result += ",";
95 // Add the feature
96 Result += V[i];
97 }
98 }
99 // Return the features string
100 return Result;
101}
102
Jim Laskeyb3302db2005-09-01 21:36:18 +0000103/// Adding features.
104void SubtargetFeatures::AddFeature(const std::string &String,
105 bool IsEnabled) {
106 // Don't add empty features
107 if (!String.empty()) {
108 // Convert to lowercase, prepend flag and add to vector
Chris Lattner54195c12005-10-23 05:26:26 +0000109 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000110 }
111}
112
Viktor Kutuzov054b52c2009-11-21 00:00:02 +0000113/// Add a set of features from the comma-separated string.
114void SubtargetFeatures::AddFeatures(const std::string &String)
115{
116 std::vector<std::string> _Features;
117
118 Split(_Features, String);
119 // Nothing is specified.
120 if (_Features.size() == 0)
121 return;
122
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.
126 AddFeature(*it);
127 }
128}
129
130/// Add a set of features from the parsed command line parameters.
131void SubtargetFeatures::AddFeatures(const cl::list<std::string> &List)
132{
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.
136 AddFeature(*it);
137 }
138}
139
Jim Laskey34bd5d52005-10-25 15:15:28 +0000140/// Find KV in array using binary search.
141template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000142 // Make the lower bound element we're looking for
143 T KV;
144 KV.Key = S.c_str();
Jim Laskeyb3302db2005-09-01 21:36:18 +0000145 // Determine the end of the array
Jim Laskey34bd5d52005-10-25 15:15:28 +0000146 const T *Hi = A + L;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000147 // Binary search the array
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000148 const T *F = std::lower_bound(A, Hi, KV);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000149 // If not found then return NULL
150 if (F == Hi || std::string(F->Key) != S) return NULL;
151 // Return the found array item
152 return F;
153}
154
Chris Lattner3e808a42005-10-23 22:23:13 +0000155/// getLongestEntryLength - Return the length of the longest entry in the table.
156///
157static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
158 size_t Size) {
159 size_t MaxLen = 0;
160 for (size_t i = 0; i < Size; i++)
161 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
162 return MaxLen;
163}
164
Jim Laskey839615a2005-09-02 19:27:43 +0000165/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000166///
Chris Lattner3e808a42005-10-23 22:23:13 +0000167static 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);
Chris Lattnerba76c212005-10-23 05:33:39 +0000172
Chris Lattner3e808a42005-10-23 22:23:13 +0000173 // Print the CPU table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000174 errs() << "Available CPUs for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000175 for (size_t i = 0; i != CPUTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000176 errs() << " " << CPUTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000177 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
178 << " - " << CPUTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000179 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000180
181 // Print the Feature table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000182 errs() << "Available features for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000183 for (size_t i = 0; i != FeatTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000184 errs() << " " << FeatTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000185 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
186 << " - " << FeatTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000187 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000188
Chris Lattnere0c86af2009-08-23 21:41:43 +0000189 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +0000190 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Chris Lattner54195c12005-10-23 05:26:26 +0000191 exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000192}
193
Chris Lattner54195c12005-10-23 05:26:26 +0000194//===----------------------------------------------------------------------===//
195// SubtargetFeatures Implementation
196//===----------------------------------------------------------------------===//
197
198SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
199 // Break up string into separate features
200 Split(Features, Initial);
201}
202
203
204std::string SubtargetFeatures::getString() const {
205 return Join(Features);
206}
207void SubtargetFeatures::setString(const std::string &Initial) {
208 // Throw out old features
209 Features.clear();
210 // Break up string into separate features
211 Split(Features, LowercaseString(Initial));
212}
213
Jim Laskey34bd5d52005-10-25 15:15:28 +0000214
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000215/// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
Chris Lattner54195c12005-10-23 05:26:26 +0000216/// clears CPU.
217void SubtargetFeatures::setCPU(const std::string &String) {
218 Features[0] = LowercaseString(String);
219}
220
221
Jim Laskey34bd5d52005-10-25 15:15:28 +0000222/// setCPUIfNone - Setting CPU string only if no string is set.
Chris Lattner54195c12005-10-23 05:26:26 +0000223///
Jim Laskey34bd5d52005-10-25 15:15:28 +0000224void SubtargetFeatures::setCPUIfNone(const std::string &String) {
225 if (Features[0].empty()) setCPU(String);
226}
227
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000228/// getCPU - Returns current CPU.
229///
230const std::string & SubtargetFeatures::getCPU() const {
231 return Features[0];
232}
233
234
Bill Wendling4222d802007-05-04 20:38:40 +0000235/// SetImpliedBits - For each feature that is (transitively) implied by this
236/// feature, set it.
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000237///
Bill Wendling4222d802007-05-04 20:38:40 +0000238static
239void 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];
244
245 if (FeatureEntry->Value == FE.Value) continue;
246
247 if (FeatureEntry->Implies & FE.Value) {
248 Bits |= FE.Value;
249 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
250 }
251 }
252}
253
254/// ClearImpliedBits - For each feature that (transitively) implies this
255/// feature, clear it.
256///
257static
258void 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];
263
264 if (FeatureEntry->Value == FE.Value) continue;
265
266 if (FE.Implies & FeatureEntry->Value) {
267 Bits &= ~FE.Value;
268 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
269 }
270 }
271}
Jim Laskey34bd5d52005-10-25 15:15:28 +0000272
273/// getBits - Get feature bits.
274///
275uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
276 size_t CPUTableSize,
277 const SubtargetFeatureKV *FeatureTable,
278 size_t FeatureTableSize) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000279 assert(CPUTable && "missing CPU table");
280 assert(FeatureTable && "missing features table");
281#ifndef NDEBUG
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");
285 }
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");
289 }
290#endif
Jim Laskeyb3302db2005-09-01 21:36:18 +0000291 uint32_t Bits = 0; // Resulting bits
Chris Lattner3e808a42005-10-23 22:23:13 +0000292
Jim Laskey34bd5d52005-10-25 15:15:28 +0000293 // Check if help is needed
294 if (Features[0] == "help")
Chris Lattner3e808a42005-10-23 22:23:13 +0000295 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
296
Jim Laskeyb3302db2005-09-01 21:36:18 +0000297 // Find CPU entry
298 const SubtargetFeatureKV *CPUEntry =
299 Find(Features[0], CPUTable, CPUTableSize);
300 // If there is a match
301 if (CPUEntry) {
302 // Set base feature bits
303 Bits = CPUEntry->Value;
Bill Wendling1a636de2007-06-27 23:34:06 +0000304
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);
310 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000311 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000312 errs() << "'" << Features[0]
313 << "' is not a recognized processor for this target"
314 << " (ignoring processor)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000315 }
316 // Iterate through each feature
317 for (size_t i = 1; i < Features.size(); i++) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000318 const std::string &Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000319
Jim Laskey839615a2005-09-02 19:27:43 +0000320 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000321 if (Feature == "+help")
322 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
323
Jim Laskeyb3302db2005-09-01 21:36:18 +0000324 // Find feature in table.
325 const SubtargetFeatureKV *FeatureEntry =
326 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
327 // If there is a match
328 if (FeatureEntry) {
329 // Enable/disable feature in bits
Bill Wendling4222d802007-05-04 20:38:40 +0000330 if (isEnabled(Feature)) {
331 Bits |= FeatureEntry->Value;
332
333 // For each feature that this implies, set it.
334 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
335 } else {
336 Bits &= ~FeatureEntry->Value;
337
338 // For each feature that implies this, clear it.
339 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
340 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000341 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000342 errs() << "'" << Feature
343 << "' is not a recognized feature for this target"
344 << " (ignoring feature)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000345 }
346 }
Bill Wendling4222d802007-05-04 20:38:40 +0000347
Jim Laskeyb3302db2005-09-01 21:36:18 +0000348 return Bits;
349}
350
Jim Laskey34bd5d52005-10-25 15:15:28 +0000351/// Get info pointer
352void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table,
353 size_t TableSize) {
354 assert(Table && "missing table");
355#ifndef NDEBUG
356 for (size_t i = 1; i < TableSize; i++) {
357 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
358 }
359#endif
360
361 // Find entry
362 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
363
364 if (Entry) {
365 return Entry->Value;
366 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000367 errs() << "'" << Features[0]
368 << "' is not a recognized processor for this target"
369 << " (ignoring processor)\n";
Jim Laskey34bd5d52005-10-25 15:15:28 +0000370 return NULL;
371 }
372}
373
374/// print - Print feature string.
375///
Chris Lattnere0c86af2009-08-23 21:41:43 +0000376void SubtargetFeatures::print(raw_ostream &OS) const {
377 for (size_t i = 0, e = Features.size(); i != e; ++i)
Jim Laskeyb3302db2005-09-01 21:36:18 +0000378 OS << Features[i] << " ";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000379 OS << "\n";
380}
381
Jim Laskey34bd5d52005-10-25 15:15:28 +0000382/// dump - Dump feature info.
383///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000384void SubtargetFeatures::dump() const {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000385 print(errs());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000386}
Viktor Kutuzove823db82009-11-18 20:20:05 +0000387
388/// getDefaultSubtargetFeatures - Return a string listing
389/// the features associated with the target triple.
390///
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>.
394///
395std::string SubtargetFeatures::getDefaultSubtargetFeatures(
396 const Triple& Triple) {
397 switch (Triple.getVendor()) {
398 case Triple::Apple:
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");
404 default:
405 break;
406 }
407 break;
408 default:
409 break;
410 }
411
412 return std::string("");
413}