blob: 4f23a85d62ec60509769d825b8887035523a1a3c [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"
Benjamin Kramer962bad72011-10-16 16:30:34 +000016#include "llvm/Support/Format.h"
Chris Lattnere0c86af2009-08-23 21:41:43 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattner54195c12005-10-23 05:26:26 +000018#include "llvm/ADT/StringExtras.h"
Jim Laskeyb3302db2005-09-01 21:36:18 +000019#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000020#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000021#include <cctype>
Nick Lewycky476b2422010-12-19 20:43:38 +000022#include <cstdlib>
Jim Laskeyb3302db2005-09-01 21:36:18 +000023using namespace llvm;
24
Chris Lattner54195c12005-10-23 05:26:26 +000025//===----------------------------------------------------------------------===//
26// Static Helper Functions
27//===----------------------------------------------------------------------===//
28
29/// hasFlag - Determine if a feature has a flag; '+' or '-'
30///
Evan Chenge1bff382011-07-01 00:23:10 +000031static inline bool hasFlag(const StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000032 assert(!Feature.empty() && "Empty string");
33 // Get first character
34 char Ch = Feature[0];
35 // Check if first character is '+' or '-' flag
36 return Ch == '+' || Ch =='-';
37}
38
39/// StripFlag - Return string stripped of flag.
40///
Evan Chenge1bff382011-07-01 00:23:10 +000041static inline std::string StripFlag(const StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000042 return hasFlag(Feature) ? Feature.substr(1) : Feature;
43}
44
45/// isEnabled - Return true if enable flag; '+'.
46///
Evan Chenge1bff382011-07-01 00:23:10 +000047static inline bool isEnabled(const StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000048 assert(!Feature.empty() && "Empty string");
49 // Get first character
50 char Ch = Feature[0];
51 // Check if first character is '+' for enabled
52 return Ch == '+';
53}
54
55/// PrependFlag - Return a string with a prepended flag; '+' or '-'.
56///
Francois Pichet24e11af2011-07-01 09:23:41 +000057static inline std::string PrependFlag(const StringRef Feature,
Evan Chenge1bff382011-07-01 00:23:10 +000058 bool IsEnabled) {
Chris Lattner54195c12005-10-23 05:26:26 +000059 assert(!Feature.empty() && "Empty string");
Evan Chenge1bff382011-07-01 00:23:10 +000060 if (hasFlag(Feature))
61 return Feature;
62 std::string Prefix = IsEnabled ? "+" : "-";
63 Prefix += Feature;
Francois Pichet24e11af2011-07-01 09:23:41 +000064 return Prefix;
Chris Lattner54195c12005-10-23 05:26:26 +000065}
66
67/// Split - Splits a string of comma separated items in to a vector of strings.
68///
Evan Chenge1bff382011-07-01 00:23:10 +000069static void Split(std::vector<std::string> &V, const StringRef S) {
Evan Cheng276365d2011-06-30 01:53:36 +000070 if (S.empty())
71 return;
72
Jim Laskeyb3302db2005-09-01 21:36:18 +000073 // Start at beginning of string.
74 size_t Pos = 0;
75 while (true) {
76 // Find the next comma
77 size_t Comma = S.find(',', Pos);
Dan Gohmanf451cb82010-02-10 16:03:48 +000078 // If no comma found then the rest of the string is used
Jim Laskeyb3302db2005-09-01 21:36:18 +000079 if (Comma == std::string::npos) {
80 // Add string to vector
81 V.push_back(S.substr(Pos));
82 break;
83 }
84 // Otherwise add substring to vector
85 V.push_back(S.substr(Pos, Comma - Pos));
86 // Advance to next item
87 Pos = Comma + 1;
88 }
89}
90
91/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner54195c12005-10-23 05:26:26 +000092///
93static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000094 // Start with empty string.
95 std::string Result;
96 // If the vector is not empty
97 if (!V.empty()) {
Evan Cheng276365d2011-06-30 01:53:36 +000098 // Start with the first feature
Jim Laskeyb3302db2005-09-01 21:36:18 +000099 Result = V[0];
100 // For each successive feature
101 for (size_t i = 1; i < V.size(); i++) {
102 // Add a comma
103 Result += ",";
104 // Add the feature
105 Result += V[i];
106 }
107 }
108 // Return the features string
109 return Result;
110}
111
Jim Laskeyb3302db2005-09-01 21:36:18 +0000112/// Adding features.
Evan Chenge1bff382011-07-01 00:23:10 +0000113void SubtargetFeatures::AddFeature(const StringRef String,
Jim Laskeyb3302db2005-09-01 21:36:18 +0000114 bool IsEnabled) {
115 // Don't add empty features
116 if (!String.empty()) {
117 // Convert to lowercase, prepend flag and add to vector
Chris Lattner54195c12005-10-23 05:26:26 +0000118 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000119 }
120}
121
Jim Laskey34bd5d52005-10-25 15:15:28 +0000122/// Find KV in array using binary search.
Evan Chenge1bff382011-07-01 00:23:10 +0000123template<typename T> const T *Find(const StringRef S, const T *A, size_t L) {
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000124 // Make the lower bound element we're looking for
125 T KV;
Evan Chenge1bff382011-07-01 00:23:10 +0000126 KV.Key = S.data();
Jim Laskeyb3302db2005-09-01 21:36:18 +0000127 // Determine the end of the array
Jim Laskey34bd5d52005-10-25 15:15:28 +0000128 const T *Hi = A + L;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000129 // Binary search the array
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000130 const T *F = std::lower_bound(A, Hi, KV);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000131 // If not found then return NULL
Evan Cheng4d9af612011-07-01 16:59:30 +0000132 if (F == Hi || StringRef(F->Key) != S) return NULL;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000133 // Return the found array item
134 return F;
135}
136
Chris Lattner3e808a42005-10-23 22:23:13 +0000137/// getLongestEntryLength - Return the length of the longest entry in the table.
138///
139static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
140 size_t Size) {
141 size_t MaxLen = 0;
142 for (size_t i = 0; i < Size; i++)
143 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
144 return MaxLen;
145}
146
Jim Laskey839615a2005-09-02 19:27:43 +0000147/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000148///
Chris Lattner3e808a42005-10-23 22:23:13 +0000149static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
150 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
151 // Determine the length of the longest CPU and Feature entries.
152 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
153 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
Chris Lattnerba76c212005-10-23 05:33:39 +0000154
Chris Lattner3e808a42005-10-23 22:23:13 +0000155 // Print the CPU table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000156 errs() << "Available CPUs for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000157 for (size_t i = 0; i != CPUTableSize; i++)
Benjamin Kramer962bad72011-10-16 16:30:34 +0000158 errs() << format(" %-*s - %s.\n",
159 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
160 errs() << '\n';
161
Chris Lattner3e808a42005-10-23 22:23:13 +0000162 // Print the Feature table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000163 errs() << "Available features for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000164 for (size_t i = 0; i != FeatTableSize; i++)
Benjamin Kramer962bad72011-10-16 16:30:34 +0000165 errs() << format(" %-*s - %s.\n",
166 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
167 errs() << '\n';
168
Chris Lattnere0c86af2009-08-23 21:41:43 +0000169 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramer962bad72011-10-16 16:30:34 +0000170 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Nick Lewycky24021232010-12-19 20:42:43 +0000171 std::exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000172}
173
Chris Lattner54195c12005-10-23 05:26:26 +0000174//===----------------------------------------------------------------------===//
175// SubtargetFeatures Implementation
176//===----------------------------------------------------------------------===//
177
Evan Chenge1bff382011-07-01 00:23:10 +0000178SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
Chris Lattner54195c12005-10-23 05:26:26 +0000179 // Break up string into separate features
180 Split(Features, Initial);
181}
182
183
Rafael Espindola3f9b9eb2011-07-01 04:40:50 +0000184std::string SubtargetFeatures::getString() const {
Chris Lattner54195c12005-10-23 05:26:26 +0000185 return Join(Features);
186}
Chris Lattner54195c12005-10-23 05:26:26 +0000187
Bill Wendling4222d802007-05-04 20:38:40 +0000188/// SetImpliedBits - For each feature that is (transitively) implied by this
189/// feature, set it.
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000190///
Bill Wendling4222d802007-05-04 20:38:40 +0000191static
Evan Chengb6a63882011-04-15 19:35:46 +0000192void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendling4222d802007-05-04 20:38:40 +0000193 const SubtargetFeatureKV *FeatureTable,
194 size_t FeatureTableSize) {
195 for (size_t i = 0; i < FeatureTableSize; ++i) {
196 const SubtargetFeatureKV &FE = FeatureTable[i];
197
198 if (FeatureEntry->Value == FE.Value) continue;
199
200 if (FeatureEntry->Implies & FE.Value) {
201 Bits |= FE.Value;
202 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
203 }
204 }
205}
206
207/// ClearImpliedBits - For each feature that (transitively) implies this
208/// feature, clear it.
209///
210static
Evan Chengb6a63882011-04-15 19:35:46 +0000211void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendling4222d802007-05-04 20:38:40 +0000212 const SubtargetFeatureKV *FeatureTable,
213 size_t FeatureTableSize) {
214 for (size_t i = 0; i < FeatureTableSize; ++i) {
215 const SubtargetFeatureKV &FE = FeatureTable[i];
216
217 if (FeatureEntry->Value == FE.Value) continue;
218
219 if (FE.Implies & FeatureEntry->Value) {
220 Bits &= ~FE.Value;
221 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
222 }
223 }
224}
Jim Laskey34bd5d52005-10-25 15:15:28 +0000225
Evan Chengffc0e732011-07-09 05:47:46 +0000226/// ToggleFeature - Toggle a feature and returns the newly updated feature
227/// bits.
228uint64_t
229SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
230 const SubtargetFeatureKV *FeatureTable,
231 size_t FeatureTableSize) {
232 // Find feature in table.
233 const SubtargetFeatureKV *FeatureEntry =
234 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
235 // If there is a match
236 if (FeatureEntry) {
237 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
238 Bits &= ~FeatureEntry->Value;
239
240 // For each feature that implies this, clear it.
241 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
242 } else {
243 Bits |= FeatureEntry->Value;
244
245 // For each feature that this implies, set it.
246 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
247 }
248 } else {
249 errs() << "'" << Feature
250 << "' is not a recognized feature for this target"
251 << " (ignoring feature)\n";
252 }
253
254 return Bits;
255}
256
257
Evan Cheng276365d2011-06-30 01:53:36 +0000258/// getFeatureBits - Get feature bits a CPU.
Jim Laskey34bd5d52005-10-25 15:15:28 +0000259///
Evan Chenge1bff382011-07-01 00:23:10 +0000260uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
Evan Cheng276365d2011-06-30 01:53:36 +0000261 const SubtargetFeatureKV *CPUTable,
262 size_t CPUTableSize,
263 const SubtargetFeatureKV *FeatureTable,
264 size_t FeatureTableSize) {
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000265 if (!FeatureTableSize || !CPUTableSize)
266 return 0;
267
Jim Laskeyb3302db2005-09-01 21:36:18 +0000268#ifndef NDEBUG
269 for (size_t i = 1; i < CPUTableSize; i++) {
270 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
271 "CPU table is not sorted");
272 }
273 for (size_t i = 1; i < FeatureTableSize; i++) {
274 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
275 "CPU features table is not sorted");
276 }
277#endif
Evan Chengb6a63882011-04-15 19:35:46 +0000278 uint64_t Bits = 0; // Resulting bits
Chris Lattner3e808a42005-10-23 22:23:13 +0000279
Jim Laskey34bd5d52005-10-25 15:15:28 +0000280 // Check if help is needed
Evan Cheng276365d2011-06-30 01:53:36 +0000281 if (CPU == "help")
Chris Lattner3e808a42005-10-23 22:23:13 +0000282 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
283
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000284 // Find CPU entry if CPU name is specified.
285 if (!CPU.empty()) {
286 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
287 // If there is a match
288 if (CPUEntry) {
289 // Set base feature bits
290 Bits = CPUEntry->Value;
Bill Wendling1a636de2007-06-27 23:34:06 +0000291
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000292 // Set the feature implied by this CPU feature, if any.
293 for (size_t i = 0; i < FeatureTableSize; ++i) {
294 const SubtargetFeatureKV &FE = FeatureTable[i];
295 if (CPUEntry->Value & FE.Value)
296 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
297 }
298 } else {
299 errs() << "'" << CPU
300 << "' is not a recognized processor for this target"
301 << " (ignoring processor)\n";
Bill Wendling1a636de2007-06-27 23:34:06 +0000302 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000303 }
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000304
Jim Laskeyb3302db2005-09-01 21:36:18 +0000305 // Iterate through each feature
Evan Cheng276365d2011-06-30 01:53:36 +0000306 for (size_t i = 0, E = Features.size(); i < E; i++) {
Evan Chenge1bff382011-07-01 00:23:10 +0000307 const StringRef Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000308
Jim Laskey839615a2005-09-02 19:27:43 +0000309 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000310 if (Feature == "+help")
311 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
312
Jim Laskeyb3302db2005-09-01 21:36:18 +0000313 // Find feature in table.
314 const SubtargetFeatureKV *FeatureEntry =
315 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
316 // If there is a match
317 if (FeatureEntry) {
318 // Enable/disable feature in bits
Bill Wendling4222d802007-05-04 20:38:40 +0000319 if (isEnabled(Feature)) {
320 Bits |= FeatureEntry->Value;
321
322 // For each feature that this implies, set it.
323 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
324 } else {
325 Bits &= ~FeatureEntry->Value;
326
327 // For each feature that implies this, clear it.
328 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
329 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000330 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000331 errs() << "'" << Feature
332 << "' is not a recognized feature for this target"
333 << " (ignoring feature)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000334 }
335 }
Bill Wendling4222d802007-05-04 20:38:40 +0000336
Jim Laskeyb3302db2005-09-01 21:36:18 +0000337 return Bits;
338}
339
Evan Cheng276365d2011-06-30 01:53:36 +0000340/// Get scheduling itinerary of a CPU.
Evan Chenge1bff382011-07-01 00:23:10 +0000341void *SubtargetFeatures::getItinerary(const StringRef CPU,
Evan Cheng276365d2011-06-30 01:53:36 +0000342 const SubtargetInfoKV *Table,
343 size_t TableSize) {
Jim Laskey34bd5d52005-10-25 15:15:28 +0000344 assert(Table && "missing table");
345#ifndef NDEBUG
346 for (size_t i = 1; i < TableSize; i++) {
347 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
348 }
349#endif
350
351 // Find entry
Evan Cheng276365d2011-06-30 01:53:36 +0000352 const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize);
Jim Laskey34bd5d52005-10-25 15:15:28 +0000353
354 if (Entry) {
355 return Entry->Value;
356 } else {
Evan Cheng276365d2011-06-30 01:53:36 +0000357 errs() << "'" << CPU
Chris Lattnere0c86af2009-08-23 21:41:43 +0000358 << "' is not a recognized processor for this target"
359 << " (ignoring processor)\n";
Jim Laskey34bd5d52005-10-25 15:15:28 +0000360 return NULL;
361 }
362}
363
364/// print - Print feature string.
365///
Chris Lattnere0c86af2009-08-23 21:41:43 +0000366void SubtargetFeatures::print(raw_ostream &OS) const {
367 for (size_t i = 0, e = Features.size(); i != e; ++i)
Jim Laskeyb3302db2005-09-01 21:36:18 +0000368 OS << Features[i] << " ";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000369 OS << "\n";
370}
371
Jim Laskey34bd5d52005-10-25 15:15:28 +0000372/// dump - Dump feature info.
373///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000374void SubtargetFeatures::dump() const {
David Greene9759c302010-01-05 01:29:36 +0000375 print(dbgs());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000376}
Viktor Kutuzove823db82009-11-18 20:20:05 +0000377
Bill Wendling81043ee2010-05-11 00:30:02 +0000378/// getDefaultSubtargetFeatures - Return a string listing the features
379/// associated with the target triple.
Viktor Kutuzove823db82009-11-18 20:20:05 +0000380///
381/// FIXME: This is an inelegant way of specifying the features of a
382/// subtarget. It would be better if we could encode this information
383/// into the IR. See <rdar://5972456>.
384///
Evan Cheng276365d2011-06-30 01:53:36 +0000385void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendlingf6d84812010-05-11 20:46:04 +0000386 if (Triple.getVendor() == Triple::Apple) {
387 if (Triple.getArch() == Triple::ppc) {
388 // powerpc-apple-*
389 AddFeature("altivec");
390 } else if (Triple.getArch() == Triple::ppc64) {
391 // powerpc64-apple-*
392 AddFeature("64bit");
393 AddFeature("altivec");
Viktor Kutuzove823db82009-11-18 20:20:05 +0000394 }
Bill Wendling81043ee2010-05-11 00:30:02 +0000395 }
Viktor Kutuzove823db82009-11-18 20:20:05 +0000396}