blob: 7cce0fe756ef149b7391ef7cac2817d22048162a [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;
Chandler Carruthe4405e92015-09-10 06:12:31 +000059 S.split(Tmp, ',', -1, false /* KeepEmpty */);
Eric Christopheraddf51d2014-05-13 19:55:17 +000060 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
Artyom Skrobov8c699232016-01-05 10:25:56 +0000163/// ToggleFeature - Toggle a feature and update the feature bits.
164void
165SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
Eric Christopher0e6f41c2014-05-06 21:04:27 +0000166 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000167
Evan Cheng91111d22011-07-09 05:47:46 +0000168 // Find feature in table.
169 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000170 Find(StripFlag(Feature), FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000171 // If there is a match
172 if (FeatureEntry) {
173 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
174 Bits &= ~FeatureEntry->Value;
Evan Cheng91111d22011-07-09 05:47:46 +0000175 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000176 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000177 } else {
178 Bits |= FeatureEntry->Value;
179
180 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000181 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000182 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000183 } else {
184 errs() << "'" << Feature
185 << "' is not a recognized feature for this target"
186 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000187 }
Evan Cheng91111d22011-07-09 05:47:46 +0000188}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000189
Artyom Skrobov8c699232016-01-05 10:25:56 +0000190void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
John Brawnd03d2292015-06-05 13:29:24 +0000191 ArrayRef<SubtargetFeatureKV> FeatureTable) {
192
193 assert(hasFlag(Feature));
194
195 // Find feature in table.
196 const SubtargetFeatureKV *FeatureEntry =
197 Find(StripFlag(Feature), FeatureTable);
198 // If there is a match
199 if (FeatureEntry) {
200 // Enable/disable feature in bits
201 if (isEnabled(Feature)) {
Artyom Skrobov8c699232016-01-05 10:25:56 +0000202 Bits |= FeatureEntry->Value;
John Brawnd03d2292015-06-05 13:29:24 +0000203
204 // For each feature that this implies, set it.
205 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
206 } else {
207 Bits &= ~FeatureEntry->Value;
208
209 // For each feature that implies this, clear it.
210 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
211 }
212 } else {
213 errs() << "'" << Feature
214 << "' is not a recognized feature for this target"
215 << " (ignoring feature)\n";
216 }
John Brawnd03d2292015-06-05 13:29:24 +0000217}
218
Evan Cheng91111d22011-07-09 05:47:46 +0000219
Evan Chengfe6e4052011-06-30 01:53:36 +0000220/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000221///
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000222FeatureBitset
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000223SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000224 ArrayRef<SubtargetFeatureKV> CPUTable,
225 ArrayRef<SubtargetFeatureKV> FeatureTable) {
226
227 if (CPUTable.empty() || FeatureTable.empty())
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000228 return FeatureBitset();
Evan Cheng1a72add62011-07-07 07:07:08 +0000229
Jim Laskey3fee6a52005-09-01 21:36:18 +0000230#ifndef NDEBUG
Craig Topper5f167722016-01-03 07:33:45 +0000231 assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
232 "CPU table is not sorted");
233 assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
234 "CPU features table is not sorted");
Jim Laskey3fee6a52005-09-01 21:36:18 +0000235#endif
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000236 // Resulting bits
237 FeatureBitset Bits;
Chris Lattner8ff9df22005-10-23 22:23:13 +0000238
Jim Laskeydb4621a2005-10-25 15:15:28 +0000239 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000240 if (CPU == "help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000241 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000242
Evan Cheng1a72add62011-07-07 07:07:08 +0000243 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000244 else if (!CPU.empty()) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000245 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
246
Evan Cheng1a72add62011-07-07 07:07:08 +0000247 // If there is a match
248 if (CPUEntry) {
249 // Set base feature bits
250 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000251
Evan Cheng1a72add62011-07-07 07:07:08 +0000252 // Set the feature implied by this CPU feature, if any.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000253 for (auto &FE : FeatureTable) {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000254 if ((CPUEntry->Value & FE.Value).any())
Eric Christopherdc5072d2014-05-06 20:23:04 +0000255 SetImpliedBits(Bits, &FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000256 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000257 } else {
258 errs() << "'" << CPU
259 << "' is not a recognized processor for this target"
260 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000261 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000262 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000263
Jim Laskey3fee6a52005-09-01 21:36:18 +0000264 // Iterate through each feature
Eric Christopherdc5072d2014-05-06 20:23:04 +0000265 for (auto &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000266 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000267 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000268 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000269
Artyom Skrobov8c699232016-01-05 10:25:56 +0000270 ApplyFeatureFlag(Bits, Feature, FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000271 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000272
Jim Laskey3fee6a52005-09-01 21:36:18 +0000273 return Bits;
274}
275
Jim Laskeydb4621a2005-10-25 15:15:28 +0000276/// print - Print feature string.
277///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000278void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000279 for (auto &F : Features)
280 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000281 OS << "\n";
282}
283
Manman Ren49d684e2012-09-12 05:06:18 +0000284#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000285/// dump - Dump feature info.
286///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000287void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000288 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000289}
Manman Renc3366cc2012-09-06 19:55:56 +0000290#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000291
Rafael Espindola41d630f2013-10-07 13:34:05 +0000292/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000293///
294/// FIXME: This is an inelegant way of specifying the features of a
295/// subtarget. It would be better if we could encode this information
296/// into the IR. See <rdar://5972456>.
297///
Evan Chengfe6e4052011-06-30 01:53:36 +0000298void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000299 if (Triple.getVendor() == Triple::Apple) {
300 if (Triple.getArch() == Triple::ppc) {
301 // powerpc-apple-*
302 AddFeature("altivec");
303 } else if (Triple.getArch() == Triple::ppc64) {
304 // powerpc64-apple-*
305 AddFeature("64bit");
306 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000307 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000308 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000309}