blob: 8383a181a902592894c1bf5e9adb5711a22d82ef [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
Chris Lattner18a70c32005-10-23 05:26:26 +000054/// Split - Splits a string of comma separated items in to a vector of strings.
55///
Evan Chengd8e27a52011-07-01 00:23:10 +000056static void Split(std::vector<std::string> &V, const StringRef S) {
Evan Chengfe6e4052011-06-30 01:53:36 +000057 if (S.empty())
58 return;
59
Jim Laskey3fee6a52005-09-01 21:36:18 +000060 // Start at beginning of string.
61 size_t Pos = 0;
62 while (true) {
63 // Find the next comma
64 size_t Comma = S.find(',', Pos);
Dan Gohman4a618822010-02-10 16:03:48 +000065 // If no comma found then the rest of the string is used
Jim Laskey3fee6a52005-09-01 21:36:18 +000066 if (Comma == std::string::npos) {
67 // Add string to vector
68 V.push_back(S.substr(Pos));
69 break;
70 }
71 // Otherwise add substring to vector
72 V.push_back(S.substr(Pos, Comma - Pos));
73 // Advance to next item
74 Pos = Comma + 1;
75 }
76}
77
78/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner18a70c32005-10-23 05:26:26 +000079///
80static std::string Join(const std::vector<std::string> &V) {
Jim Laskey3fee6a52005-09-01 21:36:18 +000081 // Start with empty string.
82 std::string Result;
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000083 // If the vector is not empty
Jim Laskey3fee6a52005-09-01 21:36:18 +000084 if (!V.empty()) {
Evan Chengfe6e4052011-06-30 01:53:36 +000085 // Start with the first feature
Jim Laskey3fee6a52005-09-01 21:36:18 +000086 Result = V[0];
87 // For each successive feature
88 for (size_t i = 1; i < V.size(); i++) {
89 // Add a comma
90 Result += ",";
91 // Add the feature
92 Result += V[i];
93 }
94 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000095 // Return the features string
Jim Laskey3fee6a52005-09-01 21:36:18 +000096 return Result;
97}
98
Jim Laskey3fee6a52005-09-01 21:36:18 +000099/// Adding features.
Eric Christopher7eba3f92014-05-06 02:37:26 +0000100void SubtargetFeatures::AddFeature(const StringRef String) {
101 // Don't add empty features or features we already have.
102 if (!String.empty())
103 // Convert to lowercase, prepend flag if we don't already have a flag.
104 Features.push_back(hasFlag(String) ? String.str() : "+" + String.lower());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000105}
106
Jim Laskeydb4621a2005-10-25 15:15:28 +0000107/// Find KV in array using binary search.
Artyom Skroboveab75152014-01-25 16:56:18 +0000108static const SubtargetFeatureKV *Find(StringRef S, const SubtargetFeatureKV *A,
109 size_t L) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000110 // Determine the end of the array
Artyom Skroboveab75152014-01-25 16:56:18 +0000111 const SubtargetFeatureKV *Hi = A + L;
Jim Laskey3fee6a52005-09-01 21:36:18 +0000112 // Binary search the array
Artyom Skroboveab75152014-01-25 16:56:18 +0000113 const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000114 // If not found then return NULL
Craig Topperbb694de2014-04-13 04:57:38 +0000115 if (F == Hi || StringRef(F->Key) != S) return nullptr;
Jim Laskey3fee6a52005-09-01 21:36:18 +0000116 // Return the found array item
117 return F;
118}
119
Chris Lattner8ff9df22005-10-23 22:23:13 +0000120/// getLongestEntryLength - Return the length of the longest entry in the table.
121///
122static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
123 size_t Size) {
124 size_t MaxLen = 0;
125 for (size_t i = 0; i < Size; i++)
126 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
127 return MaxLen;
128}
129
Jim Laskey27d628d2005-09-02 19:27:43 +0000130/// Display help for feature choices.
Chris Lattner18a70c32005-10-23 05:26:26 +0000131///
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000132static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
Eric Christopherfbed0442014-05-05 22:01:47 +0000133 const SubtargetFeatureKV *FeatTable,
134 size_t FeatTableSize) {
Chris Lattner8ff9df22005-10-23 22:23:13 +0000135 // Determine the length of the longest CPU and Feature entries.
136 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
137 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
Chris Lattnerf64f8402005-10-23 05:33:39 +0000138
Chris Lattner8ff9df22005-10-23 22:23:13 +0000139 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000140 errs() << "Available CPUs for this target:\n\n";
Chris Lattner8ff9df22005-10-23 22:23:13 +0000141 for (size_t i = 0; i != CPUTableSize; i++)
Benjamin Kramercc863b22011-10-16 16:30:34 +0000142 errs() << format(" %-*s - %s.\n",
143 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
144 errs() << '\n';
145
Chris Lattner8ff9df22005-10-23 22:23:13 +0000146 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000147 errs() << "Available features for this target:\n\n";
Chris Lattner8ff9df22005-10-23 22:23:13 +0000148 for (size_t i = 0; i != FeatTableSize; i++)
Benjamin Kramercc863b22011-10-16 16:30:34 +0000149 errs() << format(" %-*s - %s.\n",
150 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
151 errs() << '\n';
152
Chris Lattnerf31ef092009-08-23 21:41:43 +0000153 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000154 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey27d628d2005-09-02 19:27:43 +0000155}
156
Chris Lattner18a70c32005-10-23 05:26:26 +0000157//===----------------------------------------------------------------------===//
158// SubtargetFeatures Implementation
159//===----------------------------------------------------------------------===//
160
Evan Chengd8e27a52011-07-01 00:23:10 +0000161SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000162 // Break up string into separate features
163 Split(Features, Initial);
164}
165
166
Rafael Espindola968af4f2011-07-01 04:40:50 +0000167std::string SubtargetFeatures::getString() const {
Chris Lattner18a70c32005-10-23 05:26:26 +0000168 return Join(Features);
169}
Chris Lattner18a70c32005-10-23 05:26:26 +0000170
Bill Wendlinge6182262007-05-04 20:38:40 +0000171/// SetImpliedBits - For each feature that is (transitively) implied by this
172/// feature, set it.
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +0000173///
Bill Wendlinge6182262007-05-04 20:38:40 +0000174static
Evan Chenga2e61292011-04-15 19:35:46 +0000175void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendlinge6182262007-05-04 20:38:40 +0000176 const SubtargetFeatureKV *FeatureTable,
177 size_t FeatureTableSize) {
178 for (size_t i = 0; i < FeatureTableSize; ++i) {
179 const SubtargetFeatureKV &FE = FeatureTable[i];
180
181 if (FeatureEntry->Value == FE.Value) continue;
182
183 if (FeatureEntry->Implies & FE.Value) {
184 Bits |= FE.Value;
185 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
186 }
187 }
188}
189
190/// ClearImpliedBits - For each feature that (transitively) implies this
191/// feature, clear it.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000192///
Bill Wendlinge6182262007-05-04 20:38:40 +0000193static
Evan Chenga2e61292011-04-15 19:35:46 +0000194void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendlinge6182262007-05-04 20:38:40 +0000195 const SubtargetFeatureKV *FeatureTable,
196 size_t FeatureTableSize) {
197 for (size_t i = 0; i < FeatureTableSize; ++i) {
198 const SubtargetFeatureKV &FE = FeatureTable[i];
199
200 if (FeatureEntry->Value == FE.Value) continue;
201
202 if (FE.Implies & FeatureEntry->Value) {
203 Bits &= ~FE.Value;
204 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
205 }
206 }
207}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000208
Evan Cheng91111d22011-07-09 05:47:46 +0000209/// ToggleFeature - Toggle a feature and returns the newly updated feature
210/// bits.
211uint64_t
212SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
213 const SubtargetFeatureKV *FeatureTable,
214 size_t FeatureTableSize) {
215 // Find feature in table.
216 const SubtargetFeatureKV *FeatureEntry =
Artyom Skroboveab75152014-01-25 16:56:18 +0000217 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
Evan Cheng91111d22011-07-09 05:47:46 +0000218 // If there is a match
219 if (FeatureEntry) {
220 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
221 Bits &= ~FeatureEntry->Value;
222
223 // For each feature that implies this, clear it.
224 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
225 } else {
226 Bits |= FeatureEntry->Value;
227
228 // For each feature that this implies, set it.
229 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
230 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000231 } else {
232 errs() << "'" << Feature
233 << "' is not a recognized feature for this target"
234 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000235 }
236
237 return Bits;
238}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000239
Evan Cheng91111d22011-07-09 05:47:46 +0000240
Evan Chengfe6e4052011-06-30 01:53:36 +0000241/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000242///
Evan Chengd8e27a52011-07-01 00:23:10 +0000243uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
Evan Chengfe6e4052011-06-30 01:53:36 +0000244 const SubtargetFeatureKV *CPUTable,
245 size_t CPUTableSize,
246 const SubtargetFeatureKV *FeatureTable,
247 size_t FeatureTableSize) {
Evan Cheng1a72add62011-07-07 07:07:08 +0000248 if (!FeatureTableSize || !CPUTableSize)
249 return 0;
250
Jim Laskey3fee6a52005-09-01 21:36:18 +0000251#ifndef NDEBUG
252 for (size_t i = 1; i < CPUTableSize; i++) {
253 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
254 "CPU table is not sorted");
255 }
256 for (size_t i = 1; i < FeatureTableSize; i++) {
257 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
258 "CPU features table is not sorted");
259 }
260#endif
Evan Chenga2e61292011-04-15 19:35:46 +0000261 uint64_t Bits = 0; // Resulting bits
Chris Lattner8ff9df22005-10-23 22:23:13 +0000262
Jim Laskeydb4621a2005-10-25 15:15:28 +0000263 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000264 if (CPU == "help")
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000265 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000266
Evan Cheng1a72add62011-07-07 07:07:08 +0000267 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000268 else if (!CPU.empty()) {
Artyom Skroboveab75152014-01-25 16:56:18 +0000269 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
Evan Cheng1a72add62011-07-07 07:07:08 +0000270 // If there is a match
271 if (CPUEntry) {
272 // Set base feature bits
273 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000274
Evan Cheng1a72add62011-07-07 07:07:08 +0000275 // Set the feature implied by this CPU feature, if any.
276 for (size_t i = 0; i < FeatureTableSize; ++i) {
277 const SubtargetFeatureKV &FE = FeatureTable[i];
278 if (CPUEntry->Value & FE.Value)
279 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
280 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000281 } else {
282 errs() << "'" << CPU
283 << "' is not a recognized processor for this target"
284 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000285 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000286 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000287
Jim Laskey3fee6a52005-09-01 21:36:18 +0000288 // Iterate through each feature
Evan Chengfe6e4052011-06-30 01:53:36 +0000289 for (size_t i = 0, E = Features.size(); i < E; i++) {
Evan Chengd8e27a52011-07-01 00:23:10 +0000290 const StringRef Feature = Features[i];
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000291
Jim Laskey27d628d2005-09-02 19:27:43 +0000292 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000293 if (Feature == "+help")
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000294 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000295
Jim Laskey3fee6a52005-09-01 21:36:18 +0000296 // Find feature in table.
297 const SubtargetFeatureKV *FeatureEntry =
Artyom Skroboveab75152014-01-25 16:56:18 +0000298 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000299 // If there is a match
300 if (FeatureEntry) {
301 // Enable/disable feature in bits
Bill Wendlinge6182262007-05-04 20:38:40 +0000302 if (isEnabled(Feature)) {
303 Bits |= FeatureEntry->Value;
304
305 // For each feature that this implies, set it.
306 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
307 } else {
308 Bits &= ~FeatureEntry->Value;
309
310 // For each feature that implies this, clear it.
311 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
312 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000313 } else {
314 errs() << "'" << Feature
315 << "' is not a recognized feature for this target"
316 << " (ignoring feature)\n";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000317 }
318 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000319
Jim Laskey3fee6a52005-09-01 21:36:18 +0000320 return Bits;
321}
322
Jim Laskeydb4621a2005-10-25 15:15:28 +0000323/// print - Print feature string.
324///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000325void SubtargetFeatures::print(raw_ostream &OS) const {
326 for (size_t i = 0, e = Features.size(); i != e; ++i)
Jim Laskey3fee6a52005-09-01 21:36:18 +0000327 OS << Features[i] << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000328 OS << "\n";
329}
330
Manman Ren49d684e2012-09-12 05:06:18 +0000331#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000332/// dump - Dump feature info.
333///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000334void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000335 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000336}
Manman Renc3366cc2012-09-06 19:55:56 +0000337#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000338
Rafael Espindola41d630f2013-10-07 13:34:05 +0000339/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000340///
341/// FIXME: This is an inelegant way of specifying the features of a
342/// subtarget. It would be better if we could encode this information
343/// into the IR. See <rdar://5972456>.
344///
Evan Chengfe6e4052011-06-30 01:53:36 +0000345void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000346 if (Triple.getVendor() == Triple::Apple) {
347 if (Triple.getArch() == Triple::ppc) {
348 // powerpc-apple-*
349 AddFeature("altivec");
350 } else if (Triple.getArch() == Triple::ppc64) {
351 // powerpc64-apple-*
352 AddFeature("64bit");
353 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000354 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000355 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000356}