blob: 2fb91f2125b1bdf1a908876b60a9567f75396c5a [file] [log] [blame]
Jim Laskey3fee6a52005-09-01 21:36:18 +00001//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Laskey3fee6a52005-09-01 21:36:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SubtargetFeature interface.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng8264e272011-06-29 01:14:12 +000014#include "llvm/MC/SubtargetFeature.h"
David Greene24328b92010-01-05 01:29:36 +000015#include "llvm/Support/Debug.h"
Benjamin Kramercc863b22011-10-16 16:30:34 +000016#include "llvm/Support/Format.h"
Chris Lattnerf31ef092009-08-23 21:41:43 +000017#include "llvm/Support/raw_ostream.h"
Jim Laskey3fee6a52005-09-01 21:36:18 +000018#include <algorithm>
Jim Laskey3fee6a52005-09-01 21:36:18 +000019#include <cassert>
Jeff Cohena6dde992005-09-02 02:51:42 +000020#include <cctype>
Nick Lewycky0de20af2010-12-19 20:43:38 +000021#include <cstdlib>
Jim Laskey3fee6a52005-09-01 21:36:18 +000022using namespace llvm;
23
Chris Lattner18a70c32005-10-23 05:26:26 +000024//===----------------------------------------------------------------------===//
25// Static Helper Functions
26//===----------------------------------------------------------------------===//
27
28/// hasFlag - Determine if a feature has a flag; '+' or '-'
29///
Evan Chengd8e27a52011-07-01 00:23:10 +000030static inline bool hasFlag(const StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000031 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///
Evan Chengd8e27a52011-07-01 00:23:10 +000040static inline std::string StripFlag(const StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000041 return hasFlag(Feature) ? Feature.substr(1) : Feature;
42}
43
44/// isEnabled - Return true if enable flag; '+'.
45///
Evan Chengd8e27a52011-07-01 00:23:10 +000046static inline bool isEnabled(const StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000047 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///
Francois Pichet92da4502011-07-01 09:23:41 +000056static inline std::string PrependFlag(const StringRef Feature,
Evan Chengd8e27a52011-07-01 00:23:10 +000057 bool IsEnabled) {
Chris Lattner18a70c32005-10-23 05:26:26 +000058 assert(!Feature.empty() && "Empty string");
Evan Chengd8e27a52011-07-01 00:23:10 +000059 if (hasFlag(Feature))
60 return Feature;
61 std::string Prefix = IsEnabled ? "+" : "-";
62 Prefix += Feature;
Francois Pichet92da4502011-07-01 09:23:41 +000063 return Prefix;
Chris Lattner18a70c32005-10-23 05:26:26 +000064}
65
66/// Split - Splits a string of comma separated items in to a vector of strings.
67///
Evan Chengd8e27a52011-07-01 00:23:10 +000068static void Split(std::vector<std::string> &V, const StringRef S) {
Evan Chengfe6e4052011-06-30 01:53:36 +000069 if (S.empty())
70 return;
71
Jim Laskey3fee6a52005-09-01 21:36:18 +000072 // Start at beginning of string.
73 size_t Pos = 0;
74 while (true) {
75 // Find the next comma
76 size_t Comma = S.find(',', Pos);
Dan Gohman4a618822010-02-10 16:03:48 +000077 // If no comma found then the rest of the string is used
Jim Laskey3fee6a52005-09-01 21:36:18 +000078 if (Comma == std::string::npos) {
79 // Add string to vector
80 V.push_back(S.substr(Pos));
81 break;
82 }
83 // Otherwise add substring to vector
84 V.push_back(S.substr(Pos, Comma - Pos));
85 // Advance to next item
86 Pos = Comma + 1;
87 }
88}
89
90/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner18a70c32005-10-23 05:26:26 +000091///
92static std::string Join(const std::vector<std::string> &V) {
Jim Laskey3fee6a52005-09-01 21:36:18 +000093 // Start with empty string.
94 std::string Result;
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000095 // If the vector is not empty
Jim Laskey3fee6a52005-09-01 21:36:18 +000096 if (!V.empty()) {
Evan Chengfe6e4052011-06-30 01:53:36 +000097 // Start with the first feature
Jim Laskey3fee6a52005-09-01 21:36:18 +000098 Result = V[0];
99 // For each successive feature
100 for (size_t i = 1; i < V.size(); i++) {
101 // Add a comma
102 Result += ",";
103 // Add the feature
104 Result += V[i];
105 }
106 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000107 // Return the features string
Jim Laskey3fee6a52005-09-01 21:36:18 +0000108 return Result;
109}
110
Jim Laskey3fee6a52005-09-01 21:36:18 +0000111/// Adding features.
Evan Chengd8e27a52011-07-01 00:23:10 +0000112void SubtargetFeatures::AddFeature(const StringRef String,
Jim Laskey3fee6a52005-09-01 21:36:18 +0000113 bool IsEnabled) {
114 // Don't add empty features
115 if (!String.empty()) {
116 // Convert to lowercase, prepend flag and add to vector
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000117 Features.push_back(PrependFlag(String.lower(), IsEnabled));
Jim Laskey3fee6a52005-09-01 21:36:18 +0000118 }
119}
120
Jim Laskeydb4621a2005-10-25 15:15:28 +0000121/// Find KV in array using binary search.
Artyom Skroboveab75152014-01-25 16:56:18 +0000122static const SubtargetFeatureKV *Find(StringRef S, const SubtargetFeatureKV *A,
123 size_t L) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000124 // Determine the end of the array
Artyom Skroboveab75152014-01-25 16:56:18 +0000125 const SubtargetFeatureKV *Hi = A + L;
Jim Laskey3fee6a52005-09-01 21:36:18 +0000126 // Binary search the array
Artyom Skroboveab75152014-01-25 16:56:18 +0000127 const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000128 // If not found then return NULL
Artyom Skroboveab75152014-01-25 16:56:18 +0000129 if (F == Hi || StringRef(F->Key) != S) return NULL;
Jim Laskey3fee6a52005-09-01 21:36:18 +0000130 // Return the found array item
131 return F;
132}
133
Chris Lattner8ff9df22005-10-23 22:23:13 +0000134/// getLongestEntryLength - Return the length of the longest entry in the table.
135///
136static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
137 size_t Size) {
138 size_t MaxLen = 0;
139 for (size_t i = 0; i < Size; i++)
140 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
141 return MaxLen;
142}
143
Jim Laskey27d628d2005-09-02 19:27:43 +0000144/// Display help for feature choices.
Chris Lattner18a70c32005-10-23 05:26:26 +0000145///
Chris Lattner8ff9df22005-10-23 22:23:13 +0000146static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
147 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
148 // Determine the length of the longest CPU and Feature entries.
149 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
150 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
Chris Lattnerf64f8402005-10-23 05:33:39 +0000151
Chris Lattner8ff9df22005-10-23 22:23:13 +0000152 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000153 errs() << "Available CPUs for this target:\n\n";
Chris Lattner8ff9df22005-10-23 22:23:13 +0000154 for (size_t i = 0; i != CPUTableSize; i++)
Benjamin Kramercc863b22011-10-16 16:30:34 +0000155 errs() << format(" %-*s - %s.\n",
156 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
157 errs() << '\n';
158
Chris Lattner8ff9df22005-10-23 22:23:13 +0000159 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000160 errs() << "Available features for this target:\n\n";
Chris Lattner8ff9df22005-10-23 22:23:13 +0000161 for (size_t i = 0; i != FeatTableSize; i++)
Benjamin Kramercc863b22011-10-16 16:30:34 +0000162 errs() << format(" %-*s - %s.\n",
163 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
164 errs() << '\n';
165
Chris Lattnerf31ef092009-08-23 21:41:43 +0000166 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000167 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Nick Lewyckyb71afe82010-12-19 20:42:43 +0000168 std::exit(1);
Jim Laskey27d628d2005-09-02 19:27:43 +0000169}
170
Chris Lattner18a70c32005-10-23 05:26:26 +0000171//===----------------------------------------------------------------------===//
172// SubtargetFeatures Implementation
173//===----------------------------------------------------------------------===//
174
Evan Chengd8e27a52011-07-01 00:23:10 +0000175SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000176 // Break up string into separate features
177 Split(Features, Initial);
178}
179
180
Rafael Espindola968af4f2011-07-01 04:40:50 +0000181std::string SubtargetFeatures::getString() const {
Chris Lattner18a70c32005-10-23 05:26:26 +0000182 return Join(Features);
183}
Chris Lattner18a70c32005-10-23 05:26:26 +0000184
Bill Wendlinge6182262007-05-04 20:38:40 +0000185/// SetImpliedBits - For each feature that is (transitively) implied by this
186/// feature, set it.
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +0000187///
Bill Wendlinge6182262007-05-04 20:38:40 +0000188static
Evan Chenga2e61292011-04-15 19:35:46 +0000189void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendlinge6182262007-05-04 20:38:40 +0000190 const SubtargetFeatureKV *FeatureTable,
191 size_t FeatureTableSize) {
192 for (size_t i = 0; i < FeatureTableSize; ++i) {
193 const SubtargetFeatureKV &FE = FeatureTable[i];
194
195 if (FeatureEntry->Value == FE.Value) continue;
196
197 if (FeatureEntry->Implies & FE.Value) {
198 Bits |= FE.Value;
199 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
200 }
201 }
202}
203
204/// ClearImpliedBits - For each feature that (transitively) implies this
205/// feature, clear it.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000206///
Bill Wendlinge6182262007-05-04 20:38:40 +0000207static
Evan Chenga2e61292011-04-15 19:35:46 +0000208void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendlinge6182262007-05-04 20:38:40 +0000209 const SubtargetFeatureKV *FeatureTable,
210 size_t FeatureTableSize) {
211 for (size_t i = 0; i < FeatureTableSize; ++i) {
212 const SubtargetFeatureKV &FE = FeatureTable[i];
213
214 if (FeatureEntry->Value == FE.Value) continue;
215
216 if (FE.Implies & FeatureEntry->Value) {
217 Bits &= ~FE.Value;
218 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
219 }
220 }
221}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000222
Evan Cheng91111d22011-07-09 05:47:46 +0000223/// ToggleFeature - Toggle a feature and returns the newly updated feature
224/// bits.
225uint64_t
226SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
227 const SubtargetFeatureKV *FeatureTable,
228 size_t FeatureTableSize) {
229 // Find feature in table.
230 const SubtargetFeatureKV *FeatureEntry =
Artyom Skroboveab75152014-01-25 16:56:18 +0000231 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
Evan Cheng91111d22011-07-09 05:47:46 +0000232 // If there is a match
233 if (FeatureEntry) {
234 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
235 Bits &= ~FeatureEntry->Value;
236
237 // For each feature that implies this, clear it.
238 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
239 } else {
240 Bits |= FeatureEntry->Value;
241
242 // For each feature that this implies, set it.
243 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
244 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000245 } else {
246 errs() << "'" << Feature
247 << "' is not a recognized feature for this target"
248 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000249 }
250
251 return Bits;
252}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000253
Evan Cheng91111d22011-07-09 05:47:46 +0000254
Evan Chengfe6e4052011-06-30 01:53:36 +0000255/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000256///
Evan Chengd8e27a52011-07-01 00:23:10 +0000257uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
Evan Chengfe6e4052011-06-30 01:53:36 +0000258 const SubtargetFeatureKV *CPUTable,
259 size_t CPUTableSize,
260 const SubtargetFeatureKV *FeatureTable,
261 size_t FeatureTableSize) {
Evan Cheng1a72add62011-07-07 07:07:08 +0000262 if (!FeatureTableSize || !CPUTableSize)
263 return 0;
264
Jim Laskey3fee6a52005-09-01 21:36:18 +0000265#ifndef NDEBUG
266 for (size_t i = 1; i < CPUTableSize; i++) {
267 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
268 "CPU table is not sorted");
269 }
270 for (size_t i = 1; i < FeatureTableSize; i++) {
271 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
272 "CPU features table is not sorted");
273 }
274#endif
Evan Chenga2e61292011-04-15 19:35:46 +0000275 uint64_t Bits = 0; // Resulting bits
Chris Lattner8ff9df22005-10-23 22:23:13 +0000276
Jim Laskeydb4621a2005-10-25 15:15:28 +0000277 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000278 if (CPU == "help")
Chris Lattner8ff9df22005-10-23 22:23:13 +0000279 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000280
Evan Cheng1a72add62011-07-07 07:07:08 +0000281 // Find CPU entry if CPU name is specified.
282 if (!CPU.empty()) {
Artyom Skroboveab75152014-01-25 16:56:18 +0000283 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
Evan Cheng1a72add62011-07-07 07:07:08 +0000284 // If there is a match
285 if (CPUEntry) {
286 // Set base feature bits
287 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000288
Evan Cheng1a72add62011-07-07 07:07:08 +0000289 // Set the feature implied by this CPU feature, if any.
290 for (size_t i = 0; i < FeatureTableSize; ++i) {
291 const SubtargetFeatureKV &FE = FeatureTable[i];
292 if (CPUEntry->Value & FE.Value)
293 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
294 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000295 } else {
296 errs() << "'" << CPU
297 << "' is not a recognized processor for this target"
298 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000299 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000300 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000301
Jim Laskey3fee6a52005-09-01 21:36:18 +0000302 // Iterate through each feature
Evan Chengfe6e4052011-06-30 01:53:36 +0000303 for (size_t i = 0, E = Features.size(); i < E; i++) {
Evan Chengd8e27a52011-07-01 00:23:10 +0000304 const StringRef Feature = Features[i];
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000305
Jim Laskey27d628d2005-09-02 19:27:43 +0000306 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000307 if (Feature == "+help")
308 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000309
Jim Laskey3fee6a52005-09-01 21:36:18 +0000310 // Find feature in table.
311 const SubtargetFeatureKV *FeatureEntry =
Artyom Skroboveab75152014-01-25 16:56:18 +0000312 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000313 // If there is a match
314 if (FeatureEntry) {
315 // Enable/disable feature in bits
Bill Wendlinge6182262007-05-04 20:38:40 +0000316 if (isEnabled(Feature)) {
317 Bits |= FeatureEntry->Value;
318
319 // For each feature that this implies, set it.
320 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
321 } else {
322 Bits &= ~FeatureEntry->Value;
323
324 // For each feature that implies this, clear it.
325 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
326 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000327 } else {
328 errs() << "'" << Feature
329 << "' is not a recognized feature for this target"
330 << " (ignoring feature)\n";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000331 }
332 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000333
Jim Laskey3fee6a52005-09-01 21:36:18 +0000334 return Bits;
335}
336
Jim Laskeydb4621a2005-10-25 15:15:28 +0000337/// print - Print feature string.
338///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000339void SubtargetFeatures::print(raw_ostream &OS) const {
340 for (size_t i = 0, e = Features.size(); i != e; ++i)
Jim Laskey3fee6a52005-09-01 21:36:18 +0000341 OS << Features[i] << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000342 OS << "\n";
343}
344
Manman Ren49d684e2012-09-12 05:06:18 +0000345#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000346/// dump - Dump feature info.
347///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000348void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000349 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000350}
Manman Renc3366cc2012-09-06 19:55:56 +0000351#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000352
Rafael Espindola41d630f2013-10-07 13:34:05 +0000353/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000354///
355/// FIXME: This is an inelegant way of specifying the features of a
356/// subtarget. It would be better if we could encode this information
357/// into the IR. See <rdar://5972456>.
358///
Evan Chengfe6e4052011-06-30 01:53:36 +0000359void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000360 if (Triple.getVendor() == Triple::Apple) {
361 if (Triple.getArch() == Triple::ppc) {
362 // powerpc-apple-*
363 AddFeature("altivec");
364 } else if (Triple.getArch() == Triple::ppc64) {
365 // powerpc64-apple-*
366 AddFeature("64bit");
367 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000368 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000369 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000370}