blob: a97cd1db693207f92d0e067dcec653ae42ceca65 [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"
Mehdi Aminib550cb12016-04-18 09:17:29 +000015#include "llvm/ADT/ArrayRef.h"
Benjamin Kramer5377ad62015-05-28 11:45:32 +000016#include "llvm/ADT/StringExtras.h"
David Greene24328b92010-01-05 01:29:36 +000017#include "llvm/Support/Debug.h"
Benjamin Kramercc863b22011-10-16 16:30:34 +000018#include "llvm/Support/Format.h"
Chris Lattnerf31ef092009-08-23 21:41:43 +000019#include "llvm/Support/raw_ostream.h"
Jim Laskey3fee6a52005-09-01 21:36:18 +000020#include <algorithm>
Jim Laskey3fee6a52005-09-01 21:36:18 +000021#include <cassert>
Jeff Cohena6dde992005-09-02 02:51:42 +000022#include <cctype>
Nick Lewycky0de20af2010-12-19 20:43:38 +000023#include <cstdlib>
Jim Laskey3fee6a52005-09-01 21:36:18 +000024using namespace llvm;
25
Chris Lattner18a70c32005-10-23 05:26:26 +000026//===----------------------------------------------------------------------===//
27// Static Helper Functions
28//===----------------------------------------------------------------------===//
29
30/// hasFlag - Determine if a feature has a flag; '+' or '-'
31///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000032static inline bool hasFlag(StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000033 assert(!Feature.empty() && "Empty string");
34 // Get first character
35 char Ch = Feature[0];
36 // Check if first character is '+' or '-' flag
37 return Ch == '+' || Ch =='-';
38}
39
40/// StripFlag - Return string stripped of flag.
41///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000042static inline std::string StripFlag(StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000043 return hasFlag(Feature) ? Feature.substr(1) : Feature;
44}
45
46/// isEnabled - Return true if enable flag; '+'.
47///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000048static inline bool isEnabled(StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000049 assert(!Feature.empty() && "Empty string");
50 // Get first character
51 char Ch = Feature[0];
52 // Check if first character is '+' for enabled
53 return Ch == '+';
54}
55
Chris Lattner18a70c32005-10-23 05:26:26 +000056/// Split - Splits a string of comma separated items in to a vector of strings.
57///
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000058static void Split(std::vector<std::string> &V, StringRef S) {
Hans Wennborg4fa2fd12014-08-11 02:21:32 +000059 SmallVector<StringRef, 3> Tmp;
Chandler Carruthe4405e92015-09-10 06:12:31 +000060 S.split(Tmp, ',', -1, false /* KeepEmpty */);
Eric Christopheraddf51d2014-05-13 19:55:17 +000061 V.assign(Tmp.begin(), Tmp.end());
Jim Laskey3fee6a52005-09-01 21:36:18 +000062}
63
Jim Laskey3fee6a52005-09-01 21:36:18 +000064/// Adding features.
Craig Topperf8cfe1d2015-03-31 05:52:57 +000065void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
Craig Topper28f550b2015-03-28 03:24:19 +000066 // Don't add empty features.
Eric Christopher7eba3f92014-05-06 02:37:26 +000067 if (!String.empty())
68 // Convert to lowercase, prepend flag if we don't already have a flag.
Craig Topperf8cfe1d2015-03-31 05:52:57 +000069 Features.push_back(hasFlag(String) ? String.lower()
70 : (Enable ? "+" : "-") + String.lower());
Jim Laskey3fee6a52005-09-01 21:36:18 +000071}
72
Jim Laskeydb4621a2005-10-25 15:15:28 +000073/// Find KV in array using binary search.
Eric Christopherdc5072d2014-05-06 20:23:04 +000074static const SubtargetFeatureKV *Find(StringRef S,
75 ArrayRef<SubtargetFeatureKV> A) {
Jim Laskey3fee6a52005-09-01 21:36:18 +000076 // Binary search the array
Eric Christopherdc5072d2014-05-06 20:23:04 +000077 auto F = std::lower_bound(A.begin(), A.end(), S);
Jim Laskey3fee6a52005-09-01 21:36:18 +000078 // If not found then return NULL
Eric Christopherdc5072d2014-05-06 20:23:04 +000079 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
Jim Laskey3fee6a52005-09-01 21:36:18 +000080 // Return the found array item
81 return F;
82}
83
Chris Lattner8ff9df22005-10-23 22:23:13 +000084/// getLongestEntryLength - Return the length of the longest entry in the table.
85///
Eric Christopherdc5072d2014-05-06 20:23:04 +000086static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
Chris Lattner8ff9df22005-10-23 22:23:13 +000087 size_t MaxLen = 0;
Eric Christopherdc5072d2014-05-06 20:23:04 +000088 for (auto &I : Table)
89 MaxLen = std::max(MaxLen, std::strlen(I.Key));
Chris Lattner8ff9df22005-10-23 22:23:13 +000090 return MaxLen;
91}
92
Jim Laskey27d628d2005-09-02 19:27:43 +000093/// Display help for feature choices.
Chris Lattner18a70c32005-10-23 05:26:26 +000094///
Eric Christopherdc5072d2014-05-06 20:23:04 +000095static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
96 ArrayRef<SubtargetFeatureKV> FeatTable) {
Chris Lattner8ff9df22005-10-23 22:23:13 +000097 // Determine the length of the longest CPU and Feature entries.
Eric Christopherdc5072d2014-05-06 20:23:04 +000098 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
99 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
Chris Lattnerf64f8402005-10-23 05:33:39 +0000100
Chris Lattner8ff9df22005-10-23 22:23:13 +0000101 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000102 errs() << "Available CPUs for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000103 for (auto &CPU : CPUTable)
104 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000105 errs() << '\n';
106
Chris Lattner8ff9df22005-10-23 22:23:13 +0000107 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000108 errs() << "Available features for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000109 for (auto &Feature : FeatTable)
110 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000111 errs() << '\n';
112
Chris Lattnerf31ef092009-08-23 21:41:43 +0000113 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000114 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey27d628d2005-09-02 19:27:43 +0000115}
116
Chris Lattner18a70c32005-10-23 05:26:26 +0000117//===----------------------------------------------------------------------===//
118// SubtargetFeatures Implementation
119//===----------------------------------------------------------------------===//
120
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000121SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000122 // Break up string into separate features
123 Split(Features, Initial);
124}
125
126
Rafael Espindola968af4f2011-07-01 04:40:50 +0000127std::string SubtargetFeatures::getString() const {
Benjamin Kramer5377ad62015-05-28 11:45:32 +0000128 return join(Features.begin(), Features.end(), ",");
Chris Lattner18a70c32005-10-23 05:26:26 +0000129}
Chris Lattner18a70c32005-10-23 05:26:26 +0000130
Bill Wendlinge6182262007-05-04 20:38:40 +0000131/// SetImpliedBits - For each feature that is (transitively) implied by this
132/// feature, set it.
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +0000133///
Bill Wendlinge6182262007-05-04 20:38:40 +0000134static
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000135void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000136 ArrayRef<SubtargetFeatureKV> FeatureTable) {
137 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000138 if (FeatureEntry->Value == FE.Value) continue;
139
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000140 if ((FeatureEntry->Implies & FE.Value).any()) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000141 Bits |= FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000142 SetImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000143 }
144 }
145}
146
147/// ClearImpliedBits - For each feature that (transitively) implies this
148/// feature, clear it.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000149///
Bill Wendlinge6182262007-05-04 20:38:40 +0000150static
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000151void ClearImpliedBits(FeatureBitset &Bits,
152 const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000153 ArrayRef<SubtargetFeatureKV> FeatureTable) {
154 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000155 if (FeatureEntry->Value == FE.Value) continue;
156
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000157 if ((FE.Implies & FeatureEntry->Value).any()) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000158 Bits &= ~FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000159 ClearImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000160 }
161 }
162}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000163
Artyom Skrobov8c699232016-01-05 10:25:56 +0000164/// ToggleFeature - Toggle a feature and update the feature bits.
165void
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 }
Evan Cheng91111d22011-07-09 05:47:46 +0000189}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000190
Artyom Skrobov8c699232016-01-05 10:25:56 +0000191void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
John Brawnd03d2292015-06-05 13:29:24 +0000192 ArrayRef<SubtargetFeatureKV> FeatureTable) {
193
194 assert(hasFlag(Feature));
195
196 // Find feature in table.
197 const SubtargetFeatureKV *FeatureEntry =
198 Find(StripFlag(Feature), FeatureTable);
199 // If there is a match
200 if (FeatureEntry) {
201 // Enable/disable feature in bits
202 if (isEnabled(Feature)) {
Artyom Skrobov8c699232016-01-05 10:25:56 +0000203 Bits |= FeatureEntry->Value;
John Brawnd03d2292015-06-05 13:29:24 +0000204
205 // For each feature that this implies, set it.
206 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
207 } else {
208 Bits &= ~FeatureEntry->Value;
209
210 // For each feature that implies this, clear it.
211 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
212 }
213 } else {
214 errs() << "'" << Feature
215 << "' is not a recognized feature for this target"
216 << " (ignoring feature)\n";
217 }
John Brawnd03d2292015-06-05 13:29:24 +0000218}
219
Evan Cheng91111d22011-07-09 05:47:46 +0000220
Evan Chengfe6e4052011-06-30 01:53:36 +0000221/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000222///
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000223FeatureBitset
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000224SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000225 ArrayRef<SubtargetFeatureKV> CPUTable,
226 ArrayRef<SubtargetFeatureKV> FeatureTable) {
227
228 if (CPUTable.empty() || FeatureTable.empty())
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000229 return FeatureBitset();
Evan Cheng1a72add62011-07-07 07:07:08 +0000230
Jim Laskey3fee6a52005-09-01 21:36:18 +0000231#ifndef NDEBUG
Craig Topper5f167722016-01-03 07:33:45 +0000232 assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
233 "CPU table is not sorted");
234 assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
235 "CPU features table is not sorted");
Jim Laskey3fee6a52005-09-01 21:36:18 +0000236#endif
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000237 // Resulting bits
238 FeatureBitset Bits;
Chris Lattner8ff9df22005-10-23 22:23:13 +0000239
Jim Laskeydb4621a2005-10-25 15:15:28 +0000240 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000241 if (CPU == "help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000242 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000243
Evan Cheng1a72add62011-07-07 07:07:08 +0000244 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000245 else if (!CPU.empty()) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000246 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
247
Evan Cheng1a72add62011-07-07 07:07:08 +0000248 // If there is a match
249 if (CPUEntry) {
250 // Set base feature bits
251 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000252
Evan Cheng1a72add62011-07-07 07:07:08 +0000253 // Set the feature implied by this CPU feature, if any.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000254 for (auto &FE : FeatureTable) {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000255 if ((CPUEntry->Value & FE.Value).any())
Eric Christopherdc5072d2014-05-06 20:23:04 +0000256 SetImpliedBits(Bits, &FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000257 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000258 } else {
259 errs() << "'" << CPU
260 << "' is not a recognized processor for this target"
261 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000262 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000263 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000264
Jim Laskey3fee6a52005-09-01 21:36:18 +0000265 // Iterate through each feature
Eric Christopherdc5072d2014-05-06 20:23:04 +0000266 for (auto &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000267 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000268 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000269 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000270
Artyom Skrobov8c699232016-01-05 10:25:56 +0000271 ApplyFeatureFlag(Bits, Feature, FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000272 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000273
Jim Laskey3fee6a52005-09-01 21:36:18 +0000274 return Bits;
275}
276
Jim Laskeydb4621a2005-10-25 15:15:28 +0000277/// print - Print feature string.
278///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000279void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000280 for (auto &F : Features)
281 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000282 OS << "\n";
283}
284
Manman Ren49d684e2012-09-12 05:06:18 +0000285#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000286/// dump - Dump feature info.
287///
Yaron Kereneb2a2542016-01-29 20:50:44 +0000288LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000289 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000290}
Manman Renc3366cc2012-09-06 19:55:56 +0000291#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000292
Rafael Espindola41d630f2013-10-07 13:34:05 +0000293/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000294///
295/// FIXME: This is an inelegant way of specifying the features of a
296/// subtarget. It would be better if we could encode this information
297/// into the IR. See <rdar://5972456>.
298///
Evan Chengfe6e4052011-06-30 01:53:36 +0000299void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000300 if (Triple.getVendor() == Triple::Apple) {
301 if (Triple.getArch() == Triple::ppc) {
302 // powerpc-apple-*
303 AddFeature("altivec");
304 } else if (Triple.getArch() == Triple::ppc64) {
305 // powerpc64-apple-*
306 AddFeature("64bit");
307 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000308 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000309 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000310}