blob: f893104736af37d548ec9933aa66c10f686c058b [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///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000030static inline bool hasFlag(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///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000040static inline std::string StripFlag(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///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000046static inline bool isEnabled(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///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000056static void Split(std::vector<std::string> &V, StringRef S) {
Hans Wennborg4fa2fd12014-08-11 02:21:32 +000057 SmallVector<StringRef, 3> Tmp;
Eric Christopheraddf51d2014-05-13 19:55:17 +000058 S.split(Tmp, ",", -1, false /* KeepEmpty */);
59 V.assign(Tmp.begin(), Tmp.end());
Jim Laskey3fee6a52005-09-01 21:36:18 +000060}
61
62/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner18a70c32005-10-23 05:26:26 +000063///
64static std::string Join(const std::vector<std::string> &V) {
Jim Laskey3fee6a52005-09-01 21:36:18 +000065 // Start with empty string.
66 std::string Result;
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000067 // If the vector is not empty
Jim Laskey3fee6a52005-09-01 21:36:18 +000068 if (!V.empty()) {
Evan Chengfe6e4052011-06-30 01:53:36 +000069 // Start with the first feature
Jim Laskey3fee6a52005-09-01 21:36:18 +000070 Result = V[0];
71 // For each successive feature
72 for (size_t i = 1; i < V.size(); i++) {
73 // Add a comma
74 Result += ",";
75 // Add the feature
76 Result += V[i];
77 }
78 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000079 // Return the features string
Jim Laskey3fee6a52005-09-01 21:36:18 +000080 return Result;
81}
82
Jim Laskey3fee6a52005-09-01 21:36:18 +000083/// Adding features.
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000084void SubtargetFeatures::AddFeature(StringRef String) {
Craig Topper28f550b2015-03-28 03:24:19 +000085 // Don't add empty features.
Eric Christopher7eba3f92014-05-06 02:37:26 +000086 if (!String.empty())
87 // Convert to lowercase, prepend flag if we don't already have a flag.
88 Features.push_back(hasFlag(String) ? String.str() : "+" + String.lower());
Jim Laskey3fee6a52005-09-01 21:36:18 +000089}
90
Jim Laskeydb4621a2005-10-25 15:15:28 +000091/// Find KV in array using binary search.
Eric Christopherdc5072d2014-05-06 20:23:04 +000092static const SubtargetFeatureKV *Find(StringRef S,
93 ArrayRef<SubtargetFeatureKV> A) {
Jim Laskey3fee6a52005-09-01 21:36:18 +000094 // Binary search the array
Eric Christopherdc5072d2014-05-06 20:23:04 +000095 auto F = std::lower_bound(A.begin(), A.end(), S);
Jim Laskey3fee6a52005-09-01 21:36:18 +000096 // If not found then return NULL
Eric Christopherdc5072d2014-05-06 20:23:04 +000097 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
Jim Laskey3fee6a52005-09-01 21:36:18 +000098 // Return the found array item
99 return F;
100}
101
Chris Lattner8ff9df22005-10-23 22:23:13 +0000102/// getLongestEntryLength - Return the length of the longest entry in the table.
103///
Eric Christopherdc5072d2014-05-06 20:23:04 +0000104static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
Chris Lattner8ff9df22005-10-23 22:23:13 +0000105 size_t MaxLen = 0;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000106 for (auto &I : Table)
107 MaxLen = std::max(MaxLen, std::strlen(I.Key));
Chris Lattner8ff9df22005-10-23 22:23:13 +0000108 return MaxLen;
109}
110
Jim Laskey27d628d2005-09-02 19:27:43 +0000111/// Display help for feature choices.
Chris Lattner18a70c32005-10-23 05:26:26 +0000112///
Eric Christopherdc5072d2014-05-06 20:23:04 +0000113static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
114 ArrayRef<SubtargetFeatureKV> FeatTable) {
Chris Lattner8ff9df22005-10-23 22:23:13 +0000115 // Determine the length of the longest CPU and Feature entries.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000116 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
117 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
Chris Lattnerf64f8402005-10-23 05:33:39 +0000118
Chris Lattner8ff9df22005-10-23 22:23:13 +0000119 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000120 errs() << "Available CPUs for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000121 for (auto &CPU : CPUTable)
122 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000123 errs() << '\n';
124
Chris Lattner8ff9df22005-10-23 22:23:13 +0000125 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000126 errs() << "Available features for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000127 for (auto &Feature : FeatTable)
128 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000129 errs() << '\n';
130
Chris Lattnerf31ef092009-08-23 21:41:43 +0000131 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000132 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey27d628d2005-09-02 19:27:43 +0000133}
134
Chris Lattner18a70c32005-10-23 05:26:26 +0000135//===----------------------------------------------------------------------===//
136// SubtargetFeatures Implementation
137//===----------------------------------------------------------------------===//
138
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000139SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000140 // Break up string into separate features
141 Split(Features, Initial);
142}
143
144
Rafael Espindola968af4f2011-07-01 04:40:50 +0000145std::string SubtargetFeatures::getString() const {
Chris Lattner18a70c32005-10-23 05:26:26 +0000146 return Join(Features);
147}
Chris Lattner18a70c32005-10-23 05:26:26 +0000148
Bill Wendlinge6182262007-05-04 20:38:40 +0000149/// SetImpliedBits - For each feature that is (transitively) implied by this
150/// feature, set it.
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +0000151///
Bill Wendlinge6182262007-05-04 20:38:40 +0000152static
Michael Kuperstein29704e72015-03-24 12:56:59 +0000153void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000154 ArrayRef<SubtargetFeatureKV> FeatureTable) {
155 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000156 if (FeatureEntry->Value == FE.Value) continue;
157
Michael Kuperstein29704e72015-03-24 12:56:59 +0000158 if (FeatureEntry->Implies & FE.Value) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000159 Bits |= FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000160 SetImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000161 }
162 }
163}
164
165/// ClearImpliedBits - For each feature that (transitively) implies this
166/// feature, clear it.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000167///
Bill Wendlinge6182262007-05-04 20:38:40 +0000168static
Michael Kuperstein29704e72015-03-24 12:56:59 +0000169void ClearImpliedBits(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
Michael Kuperstein29704e72015-03-24 12:56:59 +0000174 if (FE.Implies & FeatureEntry->Value) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000175 Bits &= ~FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000176 ClearImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000177 }
178 }
179}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000180
Evan Cheng91111d22011-07-09 05:47:46 +0000181/// ToggleFeature - Toggle a feature and returns the newly updated feature
182/// bits.
Michael Kuperstein29704e72015-03-24 12:56:59 +0000183uint64_t
184SubtargetFeatures::ToggleFeature(uint64_t Bits, StringRef Feature,
Eric Christopher0e6f41c2014-05-06 21:04:27 +0000185 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000186
Evan Cheng91111d22011-07-09 05:47:46 +0000187 // Find feature in table.
188 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000189 Find(StripFlag(Feature), FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000190 // If there is a match
191 if (FeatureEntry) {
192 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
193 Bits &= ~FeatureEntry->Value;
Michael Kuperstein29704e72015-03-24 12:56:59 +0000194
Evan Cheng91111d22011-07-09 05:47:46 +0000195 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000196 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000197 } else {
198 Bits |= FeatureEntry->Value;
199
200 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000201 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000202 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000203 } else {
204 errs() << "'" << Feature
205 << "' is not a recognized feature for this target"
206 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000207 }
208
209 return Bits;
210}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000211
Evan Cheng91111d22011-07-09 05:47:46 +0000212
Evan Chengfe6e4052011-06-30 01:53:36 +0000213/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000214///
Michael Kuperstein29704e72015-03-24 12:56:59 +0000215uint64_t
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000216SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000217 ArrayRef<SubtargetFeatureKV> CPUTable,
218 ArrayRef<SubtargetFeatureKV> FeatureTable) {
219
220 if (CPUTable.empty() || FeatureTable.empty())
Michael Kuperstein29704e72015-03-24 12:56:59 +0000221 return 0;
Evan Cheng1a72add62011-07-07 07:07:08 +0000222
Jim Laskey3fee6a52005-09-01 21:36:18 +0000223#ifndef NDEBUG
Eric Christopherdc5072d2014-05-06 20:23:04 +0000224 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000225 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
226 "CPU table is not sorted");
227 }
Eric Christopherdc5072d2014-05-06 20:23:04 +0000228 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000229 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
230 "CPU features table is not sorted");
231 }
232#endif
Michael Kuperstein29704e72015-03-24 12:56:59 +0000233 uint64_t Bits = 0; // Resulting bits
Chris Lattner8ff9df22005-10-23 22:23:13 +0000234
Jim Laskeydb4621a2005-10-25 15:15:28 +0000235 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000236 if (CPU == "help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000237 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000238
Evan Cheng1a72add62011-07-07 07:07:08 +0000239 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000240 else if (!CPU.empty()) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000241 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
242
Evan Cheng1a72add62011-07-07 07:07:08 +0000243 // If there is a match
244 if (CPUEntry) {
245 // Set base feature bits
246 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000247
Evan Cheng1a72add62011-07-07 07:07:08 +0000248 // Set the feature implied by this CPU feature, if any.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000249 for (auto &FE : FeatureTable) {
Michael Kuperstein29704e72015-03-24 12:56:59 +0000250 if (CPUEntry->Value & FE.Value)
Eric Christopherdc5072d2014-05-06 20:23:04 +0000251 SetImpliedBits(Bits, &FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000252 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000253 } else {
254 errs() << "'" << CPU
255 << "' is not a recognized processor for this target"
256 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000257 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000258 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000259
Jim Laskey3fee6a52005-09-01 21:36:18 +0000260 // Iterate through each feature
Eric Christopherdc5072d2014-05-06 20:23:04 +0000261 for (auto &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000262 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000263 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000264 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000265
Jim Laskey3fee6a52005-09-01 21:36:18 +0000266 // Find feature in table.
267 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000268 Find(StripFlag(Feature), FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000269 // If there is a match
270 if (FeatureEntry) {
271 // Enable/disable feature in bits
Bill Wendlinge6182262007-05-04 20:38:40 +0000272 if (isEnabled(Feature)) {
273 Bits |= FeatureEntry->Value;
274
275 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000276 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000277 } else {
278 Bits &= ~FeatureEntry->Value;
279
280 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000281 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000282 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000283 } else {
284 errs() << "'" << Feature
285 << "' is not a recognized feature for this target"
286 << " (ignoring feature)\n";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000287 }
288 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000289
Jim Laskey3fee6a52005-09-01 21:36:18 +0000290 return Bits;
291}
292
Jim Laskeydb4621a2005-10-25 15:15:28 +0000293/// print - Print feature string.
294///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000295void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000296 for (auto &F : Features)
297 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000298 OS << "\n";
299}
300
Manman Ren49d684e2012-09-12 05:06:18 +0000301#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000302/// dump - Dump feature info.
303///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000304void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000305 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000306}
Manman Renc3366cc2012-09-06 19:55:56 +0000307#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000308
Rafael Espindola41d630f2013-10-07 13:34:05 +0000309/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000310///
311/// FIXME: This is an inelegant way of specifying the features of a
312/// subtarget. It would be better if we could encode this information
313/// into the IR. See <rdar://5972456>.
314///
Evan Chengfe6e4052011-06-30 01:53:36 +0000315void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000316 if (Triple.getVendor() == Triple::Apple) {
317 if (Triple.getArch() == Triple::ppc) {
318 // powerpc-apple-*
319 AddFeature("altivec");
320 } else if (Triple.getArch() == Triple::ppc64) {
321 // powerpc64-apple-*
322 AddFeature("64bit");
323 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000324 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000325 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000326}