blob: 3be6d9641b3653061989151d451080de410e9f91 [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.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000108static const SubtargetFeatureKV *Find(StringRef S,
109 ArrayRef<SubtargetFeatureKV> A) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000110 // Binary search the array
Eric Christopherdc5072d2014-05-06 20:23:04 +0000111 auto F = std::lower_bound(A.begin(), A.end(), S);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000112 // If not found then return NULL
Eric Christopherdc5072d2014-05-06 20:23:04 +0000113 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
Jim Laskey3fee6a52005-09-01 21:36:18 +0000114 // Return the found array item
115 return F;
116}
117
Chris Lattner8ff9df22005-10-23 22:23:13 +0000118/// getLongestEntryLength - Return the length of the longest entry in the table.
119///
Eric Christopherdc5072d2014-05-06 20:23:04 +0000120static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
Chris Lattner8ff9df22005-10-23 22:23:13 +0000121 size_t MaxLen = 0;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000122 for (auto &I : Table)
123 MaxLen = std::max(MaxLen, std::strlen(I.Key));
Chris Lattner8ff9df22005-10-23 22:23:13 +0000124 return MaxLen;
125}
126
Jim Laskey27d628d2005-09-02 19:27:43 +0000127/// Display help for feature choices.
Chris Lattner18a70c32005-10-23 05:26:26 +0000128///
Eric Christopherdc5072d2014-05-06 20:23:04 +0000129static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
130 ArrayRef<SubtargetFeatureKV> FeatTable) {
Chris Lattner8ff9df22005-10-23 22:23:13 +0000131 // Determine the length of the longest CPU and Feature entries.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000132 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
133 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
Chris Lattnerf64f8402005-10-23 05:33:39 +0000134
Chris Lattner8ff9df22005-10-23 22:23:13 +0000135 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000136 errs() << "Available CPUs for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000137 for (auto &CPU : CPUTable)
138 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000139 errs() << '\n';
140
Chris Lattner8ff9df22005-10-23 22:23:13 +0000141 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000142 errs() << "Available features for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000143 for (auto &Feature : FeatTable)
144 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000145 errs() << '\n';
146
Chris Lattnerf31ef092009-08-23 21:41:43 +0000147 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000148 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey27d628d2005-09-02 19:27:43 +0000149}
150
Chris Lattner18a70c32005-10-23 05:26:26 +0000151//===----------------------------------------------------------------------===//
152// SubtargetFeatures Implementation
153//===----------------------------------------------------------------------===//
154
Evan Chengd8e27a52011-07-01 00:23:10 +0000155SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000156 // Break up string into separate features
157 Split(Features, Initial);
158}
159
160
Rafael Espindola968af4f2011-07-01 04:40:50 +0000161std::string SubtargetFeatures::getString() const {
Chris Lattner18a70c32005-10-23 05:26:26 +0000162 return Join(Features);
163}
Chris Lattner18a70c32005-10-23 05:26:26 +0000164
Bill Wendlinge6182262007-05-04 20:38:40 +0000165/// SetImpliedBits - For each feature that is (transitively) implied by this
166/// feature, set it.
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +0000167///
Bill Wendlinge6182262007-05-04 20:38:40 +0000168static
Evan Chenga2e61292011-04-15 19:35:46 +0000169void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000170 ArrayRef<SubtargetFeatureKV> FeatureTable) {
171 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000172 if (FeatureEntry->Value == FE.Value) continue;
173
174 if (FeatureEntry->Implies & FE.Value) {
175 Bits |= FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000176 SetImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000177 }
178 }
179}
180
181/// ClearImpliedBits - For each feature that (transitively) implies this
182/// feature, clear it.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000183///
Bill Wendlinge6182262007-05-04 20:38:40 +0000184static
Evan Chenga2e61292011-04-15 19:35:46 +0000185void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000186 ArrayRef<SubtargetFeatureKV> FeatureTable) {
187 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000188 if (FeatureEntry->Value == FE.Value) continue;
189
190 if (FE.Implies & FeatureEntry->Value) {
191 Bits &= ~FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000192 ClearImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000193 }
194 }
195}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000196
Evan Cheng91111d22011-07-09 05:47:46 +0000197/// ToggleFeature - Toggle a feature and returns the newly updated feature
198/// bits.
Eric Christopher0e6f41c2014-05-06 21:04:27 +0000199uint64_t
200SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
201 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000202
Evan Cheng91111d22011-07-09 05:47:46 +0000203 // Find feature in table.
204 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000205 Find(StripFlag(Feature), FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000206 // If there is a match
207 if (FeatureEntry) {
208 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
209 Bits &= ~FeatureEntry->Value;
210
211 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000212 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000213 } else {
214 Bits |= FeatureEntry->Value;
215
216 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000217 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000218 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000219 } else {
220 errs() << "'" << Feature
221 << "' is not a recognized feature for this target"
222 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000223 }
224
225 return Bits;
226}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000227
Evan Cheng91111d22011-07-09 05:47:46 +0000228
Evan Chengfe6e4052011-06-30 01:53:36 +0000229/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000230///
Eric Christopherdc5072d2014-05-06 20:23:04 +0000231uint64_t
232SubtargetFeatures::getFeatureBits(const StringRef CPU,
233 ArrayRef<SubtargetFeatureKV> CPUTable,
234 ArrayRef<SubtargetFeatureKV> FeatureTable) {
235
236 if (CPUTable.empty() || FeatureTable.empty())
Evan Cheng1a72add62011-07-07 07:07:08 +0000237 return 0;
238
Jim Laskey3fee6a52005-09-01 21:36:18 +0000239#ifndef NDEBUG
Eric Christopherdc5072d2014-05-06 20:23:04 +0000240 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000241 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
242 "CPU table is not sorted");
243 }
Eric Christopherdc5072d2014-05-06 20:23:04 +0000244 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000245 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
246 "CPU features table is not sorted");
247 }
248#endif
Evan Chenga2e61292011-04-15 19:35:46 +0000249 uint64_t Bits = 0; // Resulting bits
Chris Lattner8ff9df22005-10-23 22:23:13 +0000250
Jim Laskeydb4621a2005-10-25 15:15:28 +0000251 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000252 if (CPU == "help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000253 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000254
Evan Cheng1a72add62011-07-07 07:07:08 +0000255 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000256 else if (!CPU.empty()) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000257 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
258
Evan Cheng1a72add62011-07-07 07:07:08 +0000259 // If there is a match
260 if (CPUEntry) {
261 // Set base feature bits
262 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000263
Evan Cheng1a72add62011-07-07 07:07:08 +0000264 // Set the feature implied by this CPU feature, if any.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000265 for (auto &FE : FeatureTable) {
Evan Cheng1a72add62011-07-07 07:07:08 +0000266 if (CPUEntry->Value & FE.Value)
Eric Christopherdc5072d2014-05-06 20:23:04 +0000267 SetImpliedBits(Bits, &FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000268 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000269 } else {
270 errs() << "'" << CPU
271 << "' is not a recognized processor for this target"
272 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000273 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000274 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000275
Jim Laskey3fee6a52005-09-01 21:36:18 +0000276 // Iterate through each feature
Eric Christopherdc5072d2014-05-06 20:23:04 +0000277 for (auto &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000278 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000279 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000280 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000281
Jim Laskey3fee6a52005-09-01 21:36:18 +0000282 // Find feature in table.
283 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000284 Find(StripFlag(Feature), FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000285 // If there is a match
286 if (FeatureEntry) {
287 // Enable/disable feature in bits
Bill Wendlinge6182262007-05-04 20:38:40 +0000288 if (isEnabled(Feature)) {
289 Bits |= FeatureEntry->Value;
290
291 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000292 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000293 } else {
294 Bits &= ~FeatureEntry->Value;
295
296 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000297 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000298 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000299 } else {
300 errs() << "'" << Feature
301 << "' is not a recognized feature for this target"
302 << " (ignoring feature)\n";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000303 }
304 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000305
Jim Laskey3fee6a52005-09-01 21:36:18 +0000306 return Bits;
307}
308
Jim Laskeydb4621a2005-10-25 15:15:28 +0000309/// print - Print feature string.
310///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000311void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000312 for (auto &F : Features)
313 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000314 OS << "\n";
315}
316
Manman Ren49d684e2012-09-12 05:06:18 +0000317#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000318/// dump - Dump feature info.
319///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000320void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000321 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000322}
Manman Renc3366cc2012-09-06 19:55:56 +0000323#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000324
Rafael Espindola41d630f2013-10-07 13:34:05 +0000325/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000326///
327/// FIXME: This is an inelegant way of specifying the features of a
328/// subtarget. It would be better if we could encode this information
329/// into the IR. See <rdar://5972456>.
330///
Evan Chengfe6e4052011-06-30 01:53:36 +0000331void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000332 if (Triple.getVendor() == Triple::Apple) {
333 if (Triple.getArch() == Triple::ppc) {
334 // powerpc-apple-*
335 AddFeature("altivec");
336 } else if (Triple.getArch() == Triple::ppc64) {
337 // powerpc64-apple-*
338 AddFeature("64bit");
339 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000340 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000341 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000342}