blob: 76574e987cb16406564dbd8bca4cf7dd8685c150 [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
John Brawnd03d2292015-06-05 13:29:24 +0000193FeatureBitset
194SubtargetFeatures::ApplyFeatureFlag(FeatureBitset Bits, StringRef Feature,
195 ArrayRef<SubtargetFeatureKV> FeatureTable) {
196
197 assert(hasFlag(Feature));
198
199 // Find feature in table.
200 const SubtargetFeatureKV *FeatureEntry =
201 Find(StripFlag(Feature), FeatureTable);
202 // If there is a match
203 if (FeatureEntry) {
204 // Enable/disable feature in bits
205 if (isEnabled(Feature)) {
206 Bits |= FeatureEntry->Value;
207
208 // For each feature that this implies, set it.
209 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
210 } else {
211 Bits &= ~FeatureEntry->Value;
212
213 // For each feature that implies this, clear it.
214 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
215 }
216 } else {
217 errs() << "'" << Feature
218 << "' is not a recognized feature for this target"
219 << " (ignoring feature)\n";
220 }
221
222 return Bits;
223}
224
Evan Cheng91111d22011-07-09 05:47:46 +0000225
Evan Chengfe6e4052011-06-30 01:53:36 +0000226/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000227///
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000228FeatureBitset
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000229SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000230 ArrayRef<SubtargetFeatureKV> CPUTable,
231 ArrayRef<SubtargetFeatureKV> FeatureTable) {
232
233 if (CPUTable.empty() || FeatureTable.empty())
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000234 return FeatureBitset();
Evan Cheng1a72add62011-07-07 07:07:08 +0000235
Jim Laskey3fee6a52005-09-01 21:36:18 +0000236#ifndef NDEBUG
Eric Christopherdc5072d2014-05-06 20:23:04 +0000237 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000238 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
239 "CPU table is not sorted");
240 }
Eric Christopherdc5072d2014-05-06 20:23:04 +0000241 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000242 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
243 "CPU features table is not sorted");
244 }
245#endif
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000246 // Resulting bits
247 FeatureBitset Bits;
Chris Lattner8ff9df22005-10-23 22:23:13 +0000248
Jim Laskeydb4621a2005-10-25 15:15:28 +0000249 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000250 if (CPU == "help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000251 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000252
Evan Cheng1a72add62011-07-07 07:07:08 +0000253 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000254 else if (!CPU.empty()) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000255 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
256
Evan Cheng1a72add62011-07-07 07:07:08 +0000257 // If there is a match
258 if (CPUEntry) {
259 // Set base feature bits
260 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000261
Evan Cheng1a72add62011-07-07 07:07:08 +0000262 // Set the feature implied by this CPU feature, if any.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000263 for (auto &FE : FeatureTable) {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000264 if ((CPUEntry->Value & FE.Value).any())
Eric Christopherdc5072d2014-05-06 20:23:04 +0000265 SetImpliedBits(Bits, &FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000266 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000267 } else {
268 errs() << "'" << CPU
269 << "' is not a recognized processor for this target"
270 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000271 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000272 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000273
Jim Laskey3fee6a52005-09-01 21:36:18 +0000274 // Iterate through each feature
Eric Christopherdc5072d2014-05-06 20:23:04 +0000275 for (auto &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000276 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000277 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000278 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000279
John Brawnd03d2292015-06-05 13:29:24 +0000280 Bits = ApplyFeatureFlag(Bits, Feature, FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000281 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000282
Jim Laskey3fee6a52005-09-01 21:36:18 +0000283 return Bits;
284}
285
Jim Laskeydb4621a2005-10-25 15:15:28 +0000286/// print - Print feature string.
287///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000288void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000289 for (auto &F : Features)
290 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000291 OS << "\n";
292}
293
Manman Ren49d684e2012-09-12 05:06:18 +0000294#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000295/// dump - Dump feature info.
296///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000297void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000298 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000299}
Manman Renc3366cc2012-09-06 19:55:56 +0000300#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000301
Rafael Espindola41d630f2013-10-07 13:34:05 +0000302/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000303///
304/// FIXME: This is an inelegant way of specifying the features of a
305/// subtarget. It would be better if we could encode this information
306/// into the IR. See <rdar://5972456>.
307///
Evan Chengfe6e4052011-06-30 01:53:36 +0000308void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000309 if (Triple.getVendor() == Triple::Apple) {
310 if (Triple.getArch() == Triple::ppc) {
311 // powerpc-apple-*
312 AddFeature("altivec");
313 } else if (Triple.getArch() == Triple::ppc64) {
314 // powerpc64-apple-*
315 AddFeature("64bit");
316 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000317 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000318 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000319}