blob: e976a508bc2f58e177b9f447f276ebd5bf36b2e3 [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"
Benjamin Kramer5377ad62015-05-28 11:45:32 +000015#include "llvm/ADT/StringExtras.h"
David Greene24328b92010-01-05 01:29:36 +000016#include "llvm/Support/Debug.h"
Benjamin Kramercc863b22011-10-16 16:30:34 +000017#include "llvm/Support/Format.h"
Chris Lattnerf31ef092009-08-23 21:41:43 +000018#include "llvm/Support/raw_ostream.h"
Jim Laskey3fee6a52005-09-01 21:36:18 +000019#include <algorithm>
Jim Laskey3fee6a52005-09-01 21:36:18 +000020#include <cassert>
Jeff Cohena6dde992005-09-02 02:51:42 +000021#include <cctype>
Nick Lewycky0de20af2010-12-19 20:43:38 +000022#include <cstdlib>
Jim Laskey3fee6a52005-09-01 21:36:18 +000023using namespace llvm;
24
Chris Lattner18a70c32005-10-23 05:26:26 +000025//===----------------------------------------------------------------------===//
26// Static Helper Functions
27//===----------------------------------------------------------------------===//
28
29/// hasFlag - Determine if a feature has a flag; '+' or '-'
30///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000031static inline bool hasFlag(StringRef Feature) {
Chris Lattner18a70c32005-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///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000041static inline std::string StripFlag(StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000042 return hasFlag(Feature) ? Feature.substr(1) : Feature;
43}
44
45/// isEnabled - Return true if enable flag; '+'.
46///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000047static inline bool isEnabled(StringRef Feature) {
Chris Lattner18a70c32005-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
Chris Lattner18a70c32005-10-23 05:26:26 +000055/// Split - Splits a string of comma separated items in to a vector of strings.
56///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000057static void Split(std::vector<std::string> &V, StringRef S) {
Hans Wennborg4fa2fd12014-08-11 02:21:32 +000058 SmallVector<StringRef, 3> Tmp;
Eric Christopheraddf51d2014-05-13 19:55:17 +000059 S.split(Tmp, ",", -1, false /* KeepEmpty */);
60 V.assign(Tmp.begin(), Tmp.end());
Jim Laskey3fee6a52005-09-01 21:36:18 +000061}
62
Jim Laskey3fee6a52005-09-01 21:36:18 +000063/// Adding features.
Craig Topperf8cfe1d2015-03-31 05:52:57 +000064void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
Craig Topper28f550b2015-03-28 03:24:19 +000065 // Don't add empty features.
Eric Christopher7eba3f92014-05-06 02:37:26 +000066 if (!String.empty())
67 // Convert to lowercase, prepend flag if we don't already have a flag.
Craig Topperf8cfe1d2015-03-31 05:52:57 +000068 Features.push_back(hasFlag(String) ? String.lower()
69 : (Enable ? "+" : "-") + String.lower());
Jim Laskey3fee6a52005-09-01 21:36:18 +000070}
71
Jim Laskeydb4621a2005-10-25 15:15:28 +000072/// Find KV in array using binary search.
Eric Christopherdc5072d2014-05-06 20:23:04 +000073static const SubtargetFeatureKV *Find(StringRef S,
74 ArrayRef<SubtargetFeatureKV> A) {
Jim Laskey3fee6a52005-09-01 21:36:18 +000075 // Binary search the array
Eric Christopherdc5072d2014-05-06 20:23:04 +000076 auto F = std::lower_bound(A.begin(), A.end(), S);
Jim Laskey3fee6a52005-09-01 21:36:18 +000077 // If not found then return NULL
Eric Christopherdc5072d2014-05-06 20:23:04 +000078 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
Jim Laskey3fee6a52005-09-01 21:36:18 +000079 // Return the found array item
80 return F;
81}
82
Chris Lattner8ff9df22005-10-23 22:23:13 +000083/// getLongestEntryLength - Return the length of the longest entry in the table.
84///
Eric Christopherdc5072d2014-05-06 20:23:04 +000085static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
Chris Lattner8ff9df22005-10-23 22:23:13 +000086 size_t MaxLen = 0;
Eric Christopherdc5072d2014-05-06 20:23:04 +000087 for (auto &I : Table)
88 MaxLen = std::max(MaxLen, std::strlen(I.Key));
Chris Lattner8ff9df22005-10-23 22:23:13 +000089 return MaxLen;
90}
91
Jim Laskey27d628d2005-09-02 19:27:43 +000092/// Display help for feature choices.
Chris Lattner18a70c32005-10-23 05:26:26 +000093///
Eric Christopherdc5072d2014-05-06 20:23:04 +000094static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
95 ArrayRef<SubtargetFeatureKV> FeatTable) {
Chris Lattner8ff9df22005-10-23 22:23:13 +000096 // Determine the length of the longest CPU and Feature entries.
Eric Christopherdc5072d2014-05-06 20:23:04 +000097 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
98 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
Chris Lattnerf64f8402005-10-23 05:33:39 +000099
Chris Lattner8ff9df22005-10-23 22:23:13 +0000100 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000101 errs() << "Available CPUs for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000102 for (auto &CPU : CPUTable)
103 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000104 errs() << '\n';
105
Chris Lattner8ff9df22005-10-23 22:23:13 +0000106 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000107 errs() << "Available features for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000108 for (auto &Feature : FeatTable)
109 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000110 errs() << '\n';
111
Chris Lattnerf31ef092009-08-23 21:41:43 +0000112 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000113 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey27d628d2005-09-02 19:27:43 +0000114}
115
Chris Lattner18a70c32005-10-23 05:26:26 +0000116//===----------------------------------------------------------------------===//
117// SubtargetFeatures Implementation
118//===----------------------------------------------------------------------===//
119
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000120SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000121 // Break up string into separate features
122 Split(Features, Initial);
123}
124
125
Rafael Espindola968af4f2011-07-01 04:40:50 +0000126std::string SubtargetFeatures::getString() const {
Benjamin Kramer5377ad62015-05-28 11:45:32 +0000127 return join(Features.begin(), Features.end(), ",");
Chris Lattner18a70c32005-10-23 05:26:26 +0000128}
Chris Lattner18a70c32005-10-23 05:26:26 +0000129
Bill Wendlinge6182262007-05-04 20:38:40 +0000130/// SetImpliedBits - For each feature that is (transitively) implied by this
131/// feature, set it.
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +0000132///
Bill Wendlinge6182262007-05-04 20:38:40 +0000133static
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000134void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000135 ArrayRef<SubtargetFeatureKV> FeatureTable) {
136 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000137 if (FeatureEntry->Value == FE.Value) continue;
138
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000139 if ((FeatureEntry->Implies & FE.Value).any()) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000140 Bits |= FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000141 SetImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000142 }
143 }
144}
145
146/// ClearImpliedBits - For each feature that (transitively) implies this
147/// feature, clear it.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000148///
Bill Wendlinge6182262007-05-04 20:38:40 +0000149static
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000150void ClearImpliedBits(FeatureBitset &Bits,
151 const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000152 ArrayRef<SubtargetFeatureKV> FeatureTable) {
153 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000154 if (FeatureEntry->Value == FE.Value) continue;
155
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000156 if ((FE.Implies & FeatureEntry->Value).any()) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000157 Bits &= ~FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000158 ClearImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000159 }
160 }
161}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000162
Evan Cheng91111d22011-07-09 05:47:46 +0000163/// ToggleFeature - Toggle a feature and returns the newly updated feature
164/// bits.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000165FeatureBitset
166SubtargetFeatures::ToggleFeature(FeatureBitset Bits, StringRef Feature,
Eric Christopher0e6f41c2014-05-06 21:04:27 +0000167 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000168
Evan Cheng91111d22011-07-09 05:47:46 +0000169 // Find feature in table.
170 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000171 Find(StripFlag(Feature), FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000172 // If there is a match
173 if (FeatureEntry) {
174 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
175 Bits &= ~FeatureEntry->Value;
Evan Cheng91111d22011-07-09 05:47:46 +0000176 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000177 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000178 } else {
179 Bits |= FeatureEntry->Value;
180
181 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000182 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000183 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000184 } else {
185 errs() << "'" << Feature
186 << "' is not a recognized feature for this target"
187 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000188 }
189
190 return Bits;
191}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000192
Evan Cheng91111d22011-07-09 05:47:46 +0000193
Evan Chengfe6e4052011-06-30 01:53:36 +0000194/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000195///
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000196FeatureBitset
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000197SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000198 ArrayRef<SubtargetFeatureKV> CPUTable,
199 ArrayRef<SubtargetFeatureKV> FeatureTable) {
200
201 if (CPUTable.empty() || FeatureTable.empty())
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000202 return FeatureBitset();
Evan Cheng1a72add62011-07-07 07:07:08 +0000203
Jim Laskey3fee6a52005-09-01 21:36:18 +0000204#ifndef NDEBUG
Eric Christopherdc5072d2014-05-06 20:23:04 +0000205 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000206 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
207 "CPU table is not sorted");
208 }
Eric Christopherdc5072d2014-05-06 20:23:04 +0000209 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000210 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
211 "CPU features table is not sorted");
212 }
213#endif
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000214 // Resulting bits
215 FeatureBitset Bits;
Chris Lattner8ff9df22005-10-23 22:23:13 +0000216
Jim Laskeydb4621a2005-10-25 15:15:28 +0000217 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000218 if (CPU == "help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000219 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000220
Evan Cheng1a72add62011-07-07 07:07:08 +0000221 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000222 else if (!CPU.empty()) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000223 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
224
Evan Cheng1a72add62011-07-07 07:07:08 +0000225 // If there is a match
226 if (CPUEntry) {
227 // Set base feature bits
228 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000229
Evan Cheng1a72add62011-07-07 07:07:08 +0000230 // Set the feature implied by this CPU feature, if any.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000231 for (auto &FE : FeatureTable) {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000232 if ((CPUEntry->Value & FE.Value).any())
Eric Christopherdc5072d2014-05-06 20:23:04 +0000233 SetImpliedBits(Bits, &FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000234 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000235 } else {
236 errs() << "'" << CPU
237 << "' is not a recognized processor for this target"
238 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000239 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000240 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000241
Jim Laskey3fee6a52005-09-01 21:36:18 +0000242 // Iterate through each feature
Eric Christopherdc5072d2014-05-06 20:23:04 +0000243 for (auto &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000244 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000245 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000246 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000247
Jim Laskey3fee6a52005-09-01 21:36:18 +0000248 // Find feature in table.
249 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000250 Find(StripFlag(Feature), FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000251 // If there is a match
252 if (FeatureEntry) {
253 // Enable/disable feature in bits
Bill Wendlinge6182262007-05-04 20:38:40 +0000254 if (isEnabled(Feature)) {
255 Bits |= FeatureEntry->Value;
256
257 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000258 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000259 } else {
260 Bits &= ~FeatureEntry->Value;
261
262 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000263 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000264 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000265 } else {
266 errs() << "'" << Feature
267 << "' is not a recognized feature for this target"
268 << " (ignoring feature)\n";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000269 }
270 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000271
Jim Laskey3fee6a52005-09-01 21:36:18 +0000272 return Bits;
273}
274
Jim Laskeydb4621a2005-10-25 15:15:28 +0000275/// print - Print feature string.
276///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000277void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000278 for (auto &F : Features)
279 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000280 OS << "\n";
281}
282
Manman Ren49d684e2012-09-12 05:06:18 +0000283#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000284/// dump - Dump feature info.
285///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000286void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000287 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000288}
Manman Renc3366cc2012-09-06 19:55:56 +0000289#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000290
Rafael Espindola41d630f2013-10-07 13:34:05 +0000291/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000292///
293/// FIXME: This is an inelegant way of specifying the features of a
294/// subtarget. It would be better if we could encode this information
295/// into the IR. See <rdar://5972456>.
296///
Evan Chengfe6e4052011-06-30 01:53:36 +0000297void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000298 if (Triple.getVendor() == Triple::Apple) {
299 if (Triple.getArch() == Triple::ppc) {
300 // powerpc-apple-*
301 AddFeature("altivec");
302 } else if (Triple.getArch() == Triple::ppc64) {
303 // powerpc64-apple-*
304 AddFeature("64bit");
305 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000306 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000307 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000308}