blob: 3cf95b57c5dcba2f14d8699f6db4304e0bd53c23 [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"
David Greene9759c302010-01-05 01:29:36 +000015#include "llvm/Support/Debug.h"
Chris Lattnere0c86af2009-08-23 21:41:43 +000016#include "llvm/Support/raw_ostream.h"
Chris Lattner54195c12005-10-23 05:26:26 +000017#include "llvm/ADT/StringExtras.h"
Jim Laskeyb3302db2005-09-01 21:36:18 +000018#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000019#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000020#include <cctype>
Nick Lewycky476b2422010-12-19 20:43:38 +000021#include <cstdlib>
Jim Laskeyb3302db2005-09-01 21:36:18 +000022using namespace llvm;
23
Chris Lattner54195c12005-10-23 05:26:26 +000024//===----------------------------------------------------------------------===//
25// Static Helper Functions
26//===----------------------------------------------------------------------===//
27
28/// hasFlag - Determine if a feature has a flag; '+' or '-'
29///
30static 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 =='-';
36}
37
38/// StripFlag - Return string stripped of flag.
39///
40static inline std::string StripFlag(const std::string &Feature) {
41 return hasFlag(Feature) ? Feature.substr(1) : Feature;
42}
43
44/// isEnabled - Return true if enable flag; '+'.
45///
46static 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 == '+';
52}
53
54/// PrependFlag - Return a string with a prepended flag; '+' or '-'.
55///
56static 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;
61}
62
63/// Split - Splits a string of comma separated items in to a vector of strings.
64///
65static void Split(std::vector<std::string> &V, const std::string &S) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000066 // 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);
Dan Gohmanf451cb82010-02-10 16:03:48 +000071 // If no comma found then the rest of the string is used
Jim Laskeyb3302db2005-09-01 21:36:18 +000072 if (Comma == std::string::npos) {
73 // Add string to vector
74 V.push_back(S.substr(Pos));
75 break;
76 }
77 // Otherwise add substring to vector
78 V.push_back(S.substr(Pos, Comma - Pos));
79 // Advance to next item
80 Pos = Comma + 1;
81 }
82}
83
84/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner54195c12005-10-23 05:26:26 +000085///
86static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000087 // 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];
99 }
100 }
101 // Return the features string
102 return Result;
103}
104
Jim Laskeyb3302db2005-09-01 21:36:18 +0000105/// Adding features.
106void 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
Chris Lattner54195c12005-10-23 05:26:26 +0000111 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000112 }
113}
114
Jim Laskey34bd5d52005-10-25 15:15:28 +0000115/// Find KV in array using binary search.
116template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000117 // Make the lower bound element we're looking for
118 T KV;
119 KV.Key = S.c_str();
Jim Laskeyb3302db2005-09-01 21:36:18 +0000120 // Determine the end of the array
Jim Laskey34bd5d52005-10-25 15:15:28 +0000121 const T *Hi = A + L;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000122 // Binary search the array
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000123 const T *F = std::lower_bound(A, Hi, KV);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000124 // 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;
128}
129
Chris Lattner3e808a42005-10-23 22:23:13 +0000130/// getLongestEntryLength - Return the length of the longest entry in the table.
131///
132static 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;
138}
139
Jim Laskey839615a2005-09-02 19:27:43 +0000140/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000141///
Chris Lattner3e808a42005-10-23 22:23:13 +0000142static 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);
Chris Lattnerba76c212005-10-23 05:33:39 +0000147
Chris Lattner3e808a42005-10-23 22:23:13 +0000148 // Print the CPU table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000149 errs() << "Available CPUs for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000150 for (size_t i = 0; i != CPUTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000151 errs() << " " << CPUTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000152 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
153 << " - " << CPUTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000154 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000155
156 // Print the Feature table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000157 errs() << "Available features for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000158 for (size_t i = 0; i != FeatTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000159 errs() << " " << FeatTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000160 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
161 << " - " << FeatTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000162 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000163
Chris Lattnere0c86af2009-08-23 21:41:43 +0000164 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +0000165 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Nick Lewycky24021232010-12-19 20:42:43 +0000166 std::exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000167}
168
Chris Lattner54195c12005-10-23 05:26:26 +0000169//===----------------------------------------------------------------------===//
170// SubtargetFeatures Implementation
171//===----------------------------------------------------------------------===//
172
173SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
174 // Break up string into separate features
175 Split(Features, Initial);
176}
177
178
179std::string SubtargetFeatures::getString() const {
180 return Join(Features);
181}
182void 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));
187}
188
Jim Laskey34bd5d52005-10-25 15:15:28 +0000189
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000190/// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
Chris Lattner54195c12005-10-23 05:26:26 +0000191/// clears CPU.
192void SubtargetFeatures::setCPU(const std::string &String) {
193 Features[0] = LowercaseString(String);
194}
195
196
Jim Laskey34bd5d52005-10-25 15:15:28 +0000197/// setCPUIfNone - Setting CPU string only if no string is set.
Chris Lattner54195c12005-10-23 05:26:26 +0000198///
Jim Laskey34bd5d52005-10-25 15:15:28 +0000199void SubtargetFeatures::setCPUIfNone(const std::string &String) {
200 if (Features[0].empty()) setCPU(String);
201}
202
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000203/// getCPU - Returns current CPU.
204///
205const std::string & SubtargetFeatures::getCPU() const {
206 return Features[0];
207}
208
209
Bill Wendling4222d802007-05-04 20:38:40 +0000210/// SetImpliedBits - For each feature that is (transitively) implied by this
211/// feature, set it.
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000212///
Bill Wendling4222d802007-05-04 20:38:40 +0000213static
214void 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];
219
220 if (FeatureEntry->Value == FE.Value) continue;
221
222 if (FeatureEntry->Implies & FE.Value) {
223 Bits |= FE.Value;
224 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
225 }
226 }
227}
228
229/// ClearImpliedBits - For each feature that (transitively) implies this
230/// feature, clear it.
231///
232static
233void 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];
238
239 if (FeatureEntry->Value == FE.Value) continue;
240
241 if (FE.Implies & FeatureEntry->Value) {
242 Bits &= ~FE.Value;
243 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
244 }
245 }
246}
Jim Laskey34bd5d52005-10-25 15:15:28 +0000247
248/// getBits - Get feature bits.
249///
250uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
251 size_t CPUTableSize,
252 const SubtargetFeatureKV *FeatureTable,
253 size_t FeatureTableSize) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000254 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");
260 }
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");
264 }
265#endif
Jim Laskeyb3302db2005-09-01 21:36:18 +0000266 uint32_t Bits = 0; // Resulting bits
Chris Lattner3e808a42005-10-23 22:23:13 +0000267
Jim Laskey34bd5d52005-10-25 15:15:28 +0000268 // Check if help is needed
269 if (Features[0] == "help")
Chris Lattner3e808a42005-10-23 22:23:13 +0000270 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
271
Jim Laskeyb3302db2005-09-01 21:36:18 +0000272 // 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;
Bill Wendling1a636de2007-06-27 23:34:06 +0000279
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);
285 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000286 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000287 errs() << "'" << Features[0]
288 << "' is not a recognized processor for this target"
289 << " (ignoring processor)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000290 }
291 // Iterate through each feature
292 for (size_t i = 1; i < Features.size(); i++) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000293 const std::string &Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000294
Jim Laskey839615a2005-09-02 19:27:43 +0000295 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000296 if (Feature == "+help")
297 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
298
Jim Laskeyb3302db2005-09-01 21:36:18 +0000299 // 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
Bill Wendling4222d802007-05-04 20:38:40 +0000305 if (isEnabled(Feature)) {
306 Bits |= FeatureEntry->Value;
307
308 // For each feature that this implies, set it.
309 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
310 } else {
311 Bits &= ~FeatureEntry->Value;
312
313 // For each feature that implies this, clear it.
314 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
315 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000316 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000317 errs() << "'" << Feature
318 << "' is not a recognized feature for this target"
319 << " (ignoring feature)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000320 }
321 }
Bill Wendling4222d802007-05-04 20:38:40 +0000322
Jim Laskeyb3302db2005-09-01 21:36:18 +0000323 return Bits;
324}
325
Jim Laskey34bd5d52005-10-25 15:15:28 +0000326/// Get info pointer
327void *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");
333 }
334#endif
335
336 // Find entry
337 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
338
339 if (Entry) {
340 return Entry->Value;
341 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000342 errs() << "'" << Features[0]
343 << "' is not a recognized processor for this target"
344 << " (ignoring processor)\n";
Jim Laskey34bd5d52005-10-25 15:15:28 +0000345 return NULL;
346 }
347}
348
349/// print - Print feature string.
350///
Chris Lattnere0c86af2009-08-23 21:41:43 +0000351void SubtargetFeatures::print(raw_ostream &OS) const {
352 for (size_t i = 0, e = Features.size(); i != e; ++i)
Jim Laskeyb3302db2005-09-01 21:36:18 +0000353 OS << Features[i] << " ";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000354 OS << "\n";
355}
356
Jim Laskey34bd5d52005-10-25 15:15:28 +0000357/// dump - Dump feature info.
358///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000359void SubtargetFeatures::dump() const {
David Greene9759c302010-01-05 01:29:36 +0000360 print(dbgs());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000361}
Viktor Kutuzove823db82009-11-18 20:20:05 +0000362
Bill Wendling81043ee2010-05-11 00:30:02 +0000363/// getDefaultSubtargetFeatures - Return a string listing the features
364/// associated with the target triple.
Viktor Kutuzove823db82009-11-18 20:20:05 +0000365///
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>.
369///
Bill Wendling81043ee2010-05-11 00:30:02 +0000370void SubtargetFeatures::getDefaultSubtargetFeatures(const std::string &CPU,
371 const Triple& Triple) {
372 setCPU(CPU);
373
Bill Wendlingf6d84812010-05-11 20:46:04 +0000374 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");
Viktor Kutuzove823db82009-11-18 20:20:05 +0000382 }
Bill Wendling81043ee2010-05-11 00:30:02 +0000383 }
Viktor Kutuzove823db82009-11-18 20:20:05 +0000384}