blob: a6f6b138852ffc46805b422190aaa57cb56ecd3f [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
Evan Chengab8be962011-06-29 01:14:12 +000014#include "llvm/MC/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) {
Evan Cheng276365d2011-06-30 01:53:36 +000066 if (S.empty())
67 return;
68
Jim Laskeyb3302db2005-09-01 21:36:18 +000069 // Start at beginning of string.
70 size_t Pos = 0;
71 while (true) {
72 // Find the next comma
73 size_t Comma = S.find(',', Pos);
Dan Gohmanf451cb82010-02-10 16:03:48 +000074 // If no comma found then the rest of the string is used
Jim Laskeyb3302db2005-09-01 21:36:18 +000075 if (Comma == std::string::npos) {
76 // Add string to vector
77 V.push_back(S.substr(Pos));
78 break;
79 }
80 // Otherwise add substring to vector
81 V.push_back(S.substr(Pos, Comma - Pos));
82 // Advance to next item
83 Pos = Comma + 1;
84 }
85}
86
87/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner54195c12005-10-23 05:26:26 +000088///
89static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000090 // Start with empty string.
91 std::string Result;
92 // If the vector is not empty
93 if (!V.empty()) {
Evan Cheng276365d2011-06-30 01:53:36 +000094 // Start with the first feature
Jim Laskeyb3302db2005-09-01 21:36:18 +000095 Result = V[0];
96 // For each successive feature
97 for (size_t i = 1; i < V.size(); i++) {
98 // Add a comma
99 Result += ",";
100 // Add the feature
101 Result += V[i];
102 }
103 }
104 // Return the features string
105 return Result;
106}
107
Jim Laskeyb3302db2005-09-01 21:36:18 +0000108/// Adding features.
109void SubtargetFeatures::AddFeature(const std::string &String,
110 bool IsEnabled) {
111 // Don't add empty features
112 if (!String.empty()) {
113 // Convert to lowercase, prepend flag and add to vector
Chris Lattner54195c12005-10-23 05:26:26 +0000114 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000115 }
116}
117
Jim Laskey34bd5d52005-10-25 15:15:28 +0000118/// Find KV in array using binary search.
119template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000120 // Make the lower bound element we're looking for
121 T KV;
122 KV.Key = S.c_str();
Jim Laskeyb3302db2005-09-01 21:36:18 +0000123 // Determine the end of the array
Jim Laskey34bd5d52005-10-25 15:15:28 +0000124 const T *Hi = A + L;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000125 // Binary search the array
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000126 const T *F = std::lower_bound(A, Hi, KV);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000127 // If not found then return NULL
128 if (F == Hi || std::string(F->Key) != S) return NULL;
129 // Return the found array item
130 return F;
131}
132
Chris Lattner3e808a42005-10-23 22:23:13 +0000133/// getLongestEntryLength - Return the length of the longest entry in the table.
134///
135static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
136 size_t Size) {
137 size_t MaxLen = 0;
138 for (size_t i = 0; i < Size; i++)
139 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
140 return MaxLen;
141}
142
Jim Laskey839615a2005-09-02 19:27:43 +0000143/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000144///
Chris Lattner3e808a42005-10-23 22:23:13 +0000145static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
146 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
147 // Determine the length of the longest CPU and Feature entries.
148 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
149 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
Chris Lattnerba76c212005-10-23 05:33:39 +0000150
Chris Lattner3e808a42005-10-23 22:23:13 +0000151 // Print the CPU table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000152 errs() << "Available CPUs for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000153 for (size_t i = 0; i != CPUTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000154 errs() << " " << CPUTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000155 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
156 << " - " << CPUTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000157 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000158
159 // Print the Feature table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000160 errs() << "Available features for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000161 for (size_t i = 0; i != FeatTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000162 errs() << " " << FeatTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000163 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
164 << " - " << FeatTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000165 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000166
Chris Lattnere0c86af2009-08-23 21:41:43 +0000167 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +0000168 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Nick Lewycky24021232010-12-19 20:42:43 +0000169 std::exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000170}
171
Chris Lattner54195c12005-10-23 05:26:26 +0000172//===----------------------------------------------------------------------===//
173// SubtargetFeatures Implementation
174//===----------------------------------------------------------------------===//
175
176SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
177 // Break up string into separate features
178 Split(Features, Initial);
179}
180
181
182std::string SubtargetFeatures::getString() const {
183 return Join(Features);
184}
185void SubtargetFeatures::setString(const std::string &Initial) {
186 // Throw out old features
187 Features.clear();
188 // Break up string into separate features
189 Split(Features, LowercaseString(Initial));
190}
191
Bill Wendling4222d802007-05-04 20:38:40 +0000192/// SetImpliedBits - For each feature that is (transitively) implied by this
193/// feature, set it.
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000194///
Bill Wendling4222d802007-05-04 20:38:40 +0000195static
Evan Chengb6a63882011-04-15 19:35:46 +0000196void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendling4222d802007-05-04 20:38:40 +0000197 const SubtargetFeatureKV *FeatureTable,
198 size_t FeatureTableSize) {
199 for (size_t i = 0; i < FeatureTableSize; ++i) {
200 const SubtargetFeatureKV &FE = FeatureTable[i];
201
202 if (FeatureEntry->Value == FE.Value) continue;
203
204 if (FeatureEntry->Implies & FE.Value) {
205 Bits |= FE.Value;
206 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
207 }
208 }
209}
210
211/// ClearImpliedBits - For each feature that (transitively) implies this
212/// feature, clear it.
213///
214static
Evan Chengb6a63882011-04-15 19:35:46 +0000215void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendling4222d802007-05-04 20:38:40 +0000216 const SubtargetFeatureKV *FeatureTable,
217 size_t FeatureTableSize) {
218 for (size_t i = 0; i < FeatureTableSize; ++i) {
219 const SubtargetFeatureKV &FE = FeatureTable[i];
220
221 if (FeatureEntry->Value == FE.Value) continue;
222
223 if (FE.Implies & FeatureEntry->Value) {
224 Bits &= ~FE.Value;
225 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
226 }
227 }
228}
Jim Laskey34bd5d52005-10-25 15:15:28 +0000229
Evan Cheng276365d2011-06-30 01:53:36 +0000230/// getFeatureBits - Get feature bits a CPU.
Jim Laskey34bd5d52005-10-25 15:15:28 +0000231///
Evan Cheng276365d2011-06-30 01:53:36 +0000232uint64_t SubtargetFeatures::getFeatureBits(const std::string &CPU,
233 const SubtargetFeatureKV *CPUTable,
234 size_t CPUTableSize,
235 const SubtargetFeatureKV *FeatureTable,
236 size_t FeatureTableSize) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000237 assert(CPUTable && "missing CPU table");
238 assert(FeatureTable && "missing features table");
239#ifndef NDEBUG
240 for (size_t i = 1; i < CPUTableSize; i++) {
241 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
242 "CPU table is not sorted");
243 }
244 for (size_t i = 1; i < FeatureTableSize; i++) {
245 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
246 "CPU features table is not sorted");
247 }
248#endif
Evan Chengb6a63882011-04-15 19:35:46 +0000249 uint64_t Bits = 0; // Resulting bits
Chris Lattner3e808a42005-10-23 22:23:13 +0000250
Jim Laskey34bd5d52005-10-25 15:15:28 +0000251 // Check if help is needed
Evan Cheng276365d2011-06-30 01:53:36 +0000252 if (CPU == "help")
Chris Lattner3e808a42005-10-23 22:23:13 +0000253 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
254
Jim Laskeyb3302db2005-09-01 21:36:18 +0000255 // Find CPU entry
Evan Cheng276365d2011-06-30 01:53:36 +0000256 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000257 // If there is a match
258 if (CPUEntry) {
259 // Set base feature bits
260 Bits = CPUEntry->Value;
Bill Wendling1a636de2007-06-27 23:34:06 +0000261
262 // Set the feature implied by this CPU feature, if any.
263 for (size_t i = 0; i < FeatureTableSize; ++i) {
264 const SubtargetFeatureKV &FE = FeatureTable[i];
265 if (CPUEntry->Value & FE.Value)
266 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
267 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000268 } else {
Evan Cheng276365d2011-06-30 01:53:36 +0000269 errs() << "'" << CPU
Chris Lattnere0c86af2009-08-23 21:41:43 +0000270 << "' is not a recognized processor for this target"
271 << " (ignoring processor)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000272 }
273 // Iterate through each feature
Evan Cheng276365d2011-06-30 01:53:36 +0000274 for (size_t i = 0, E = Features.size(); i < E; i++) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000275 const std::string &Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000276
Jim Laskey839615a2005-09-02 19:27:43 +0000277 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000278 if (Feature == "+help")
279 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
280
Jim Laskeyb3302db2005-09-01 21:36:18 +0000281 // Find feature in table.
282 const SubtargetFeatureKV *FeatureEntry =
283 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
284 // If there is a match
285 if (FeatureEntry) {
286 // Enable/disable feature in bits
Bill Wendling4222d802007-05-04 20:38:40 +0000287 if (isEnabled(Feature)) {
288 Bits |= FeatureEntry->Value;
289
290 // For each feature that this implies, set it.
291 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
292 } else {
293 Bits &= ~FeatureEntry->Value;
294
295 // For each feature that implies this, clear it.
296 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
297 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000298 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000299 errs() << "'" << Feature
300 << "' is not a recognized feature for this target"
301 << " (ignoring feature)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000302 }
303 }
Bill Wendling4222d802007-05-04 20:38:40 +0000304
Jim Laskeyb3302db2005-09-01 21:36:18 +0000305 return Bits;
306}
307
Evan Cheng276365d2011-06-30 01:53:36 +0000308/// Get scheduling itinerary of a CPU.
309void *SubtargetFeatures::getItinerary(const std::string &CPU,
310 const SubtargetInfoKV *Table,
311 size_t TableSize) {
Jim Laskey34bd5d52005-10-25 15:15:28 +0000312 assert(Table && "missing table");
313#ifndef NDEBUG
314 for (size_t i = 1; i < TableSize; i++) {
315 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
316 }
317#endif
318
319 // Find entry
Evan Cheng276365d2011-06-30 01:53:36 +0000320 const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize);
Jim Laskey34bd5d52005-10-25 15:15:28 +0000321
322 if (Entry) {
323 return Entry->Value;
324 } else {
Evan Cheng276365d2011-06-30 01:53:36 +0000325 errs() << "'" << CPU
Chris Lattnere0c86af2009-08-23 21:41:43 +0000326 << "' is not a recognized processor for this target"
327 << " (ignoring processor)\n";
Jim Laskey34bd5d52005-10-25 15:15:28 +0000328 return NULL;
329 }
330}
331
332/// print - Print feature string.
333///
Chris Lattnere0c86af2009-08-23 21:41:43 +0000334void SubtargetFeatures::print(raw_ostream &OS) const {
335 for (size_t i = 0, e = Features.size(); i != e; ++i)
Jim Laskeyb3302db2005-09-01 21:36:18 +0000336 OS << Features[i] << " ";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000337 OS << "\n";
338}
339
Jim Laskey34bd5d52005-10-25 15:15:28 +0000340/// dump - Dump feature info.
341///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000342void SubtargetFeatures::dump() const {
David Greene9759c302010-01-05 01:29:36 +0000343 print(dbgs());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000344}
Viktor Kutuzove823db82009-11-18 20:20:05 +0000345
Bill Wendling81043ee2010-05-11 00:30:02 +0000346/// getDefaultSubtargetFeatures - Return a string listing the features
347/// associated with the target triple.
Viktor Kutuzove823db82009-11-18 20:20:05 +0000348///
349/// FIXME: This is an inelegant way of specifying the features of a
350/// subtarget. It would be better if we could encode this information
351/// into the IR. See <rdar://5972456>.
352///
Evan Cheng276365d2011-06-30 01:53:36 +0000353void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendlingf6d84812010-05-11 20:46:04 +0000354 if (Triple.getVendor() == Triple::Apple) {
355 if (Triple.getArch() == Triple::ppc) {
356 // powerpc-apple-*
357 AddFeature("altivec");
358 } else if (Triple.getArch() == Triple::ppc64) {
359 // powerpc64-apple-*
360 AddFeature("64bit");
361 AddFeature("altivec");
Viktor Kutuzove823db82009-11-18 20:20:05 +0000362 }
Bill Wendling81043ee2010-05-11 00:30:02 +0000363 }
Viktor Kutuzove823db82009-11-18 20:20:05 +0000364}