blob: 3880d883389dee680ecfe05b3ceb2fb58d71bd98 [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 Topperf8cfe1d2015-03-31 05:52:57 +000084void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
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.
Craig Topperf8cfe1d2015-03-31 05:52:57 +000088 Features.push_back(hasFlag(String) ? String.lower()
89 : (Enable ? "+" : "-") + String.lower());
Jim Laskey3fee6a52005-09-01 21:36:18 +000090}
91
Jim Laskeydb4621a2005-10-25 15:15:28 +000092/// Find KV in array using binary search.
Eric Christopherdc5072d2014-05-06 20:23:04 +000093static const SubtargetFeatureKV *Find(StringRef S,
94 ArrayRef<SubtargetFeatureKV> A) {
Jim Laskey3fee6a52005-09-01 21:36:18 +000095 // Binary search the array
Eric Christopherdc5072d2014-05-06 20:23:04 +000096 auto F = std::lower_bound(A.begin(), A.end(), S);
Jim Laskey3fee6a52005-09-01 21:36:18 +000097 // If not found then return NULL
Eric Christopherdc5072d2014-05-06 20:23:04 +000098 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
Jim Laskey3fee6a52005-09-01 21:36:18 +000099 // Return the found array item
100 return F;
101}
102
Chris Lattner8ff9df22005-10-23 22:23:13 +0000103/// getLongestEntryLength - Return the length of the longest entry in the table.
104///
Eric Christopherdc5072d2014-05-06 20:23:04 +0000105static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
Chris Lattner8ff9df22005-10-23 22:23:13 +0000106 size_t MaxLen = 0;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000107 for (auto &I : Table)
108 MaxLen = std::max(MaxLen, std::strlen(I.Key));
Chris Lattner8ff9df22005-10-23 22:23:13 +0000109 return MaxLen;
110}
111
Jim Laskey27d628d2005-09-02 19:27:43 +0000112/// Display help for feature choices.
Chris Lattner18a70c32005-10-23 05:26:26 +0000113///
Eric Christopherdc5072d2014-05-06 20:23:04 +0000114static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
115 ArrayRef<SubtargetFeatureKV> FeatTable) {
Chris Lattner8ff9df22005-10-23 22:23:13 +0000116 // Determine the length of the longest CPU and Feature entries.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000117 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
118 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
Chris Lattnerf64f8402005-10-23 05:33:39 +0000119
Chris Lattner8ff9df22005-10-23 22:23:13 +0000120 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000121 errs() << "Available CPUs for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000122 for (auto &CPU : CPUTable)
123 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000124 errs() << '\n';
125
Chris Lattner8ff9df22005-10-23 22:23:13 +0000126 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000127 errs() << "Available features for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000128 for (auto &Feature : FeatTable)
129 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000130 errs() << '\n';
131
Chris Lattnerf31ef092009-08-23 21:41:43 +0000132 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000133 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey27d628d2005-09-02 19:27:43 +0000134}
135
Chris Lattner18a70c32005-10-23 05:26:26 +0000136//===----------------------------------------------------------------------===//
137// SubtargetFeatures Implementation
138//===----------------------------------------------------------------------===//
139
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000140SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000141 // Break up string into separate features
142 Split(Features, Initial);
143}
144
145
Rafael Espindola968af4f2011-07-01 04:40:50 +0000146std::string SubtargetFeatures::getString() const {
Chris Lattner18a70c32005-10-23 05:26:26 +0000147 return Join(Features);
148}
Chris Lattner18a70c32005-10-23 05:26:26 +0000149
Bill Wendlinge6182262007-05-04 20:38:40 +0000150/// SetImpliedBits - For each feature that is (transitively) implied by this
151/// feature, set it.
Anton Korobeynikov08bf4c02009-05-23 19:50:50 +0000152///
Bill Wendlinge6182262007-05-04 20:38:40 +0000153static
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000154void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000155 ArrayRef<SubtargetFeatureKV> FeatureTable) {
156 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000157 if (FeatureEntry->Value == FE.Value) continue;
158
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000159 if (FeatureEntry->Implies & FE.Value) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000160 Bits |= FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000161 SetImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000162 }
163 }
164}
165
166/// ClearImpliedBits - For each feature that (transitively) implies this
167/// feature, clear it.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000168///
Bill Wendlinge6182262007-05-04 20:38:40 +0000169static
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000170void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000171 ArrayRef<SubtargetFeatureKV> FeatureTable) {
172 for (auto &FE : FeatureTable) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000173 if (FeatureEntry->Value == FE.Value) continue;
174
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000175 if (FE.Implies & FeatureEntry->Value) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000176 Bits &= ~FE.Value;
Eric Christopherdc5072d2014-05-06 20:23:04 +0000177 ClearImpliedBits(Bits, &FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000178 }
179 }
180}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000181
Evan Cheng91111d22011-07-09 05:47:46 +0000182/// ToggleFeature - Toggle a feature and returns the newly updated feature
183/// bits.
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000184uint64_t
185SubtargetFeatures::ToggleFeature(uint64_t Bits, StringRef Feature,
Eric Christopher0e6f41c2014-05-06 21:04:27 +0000186 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000187
Evan Cheng91111d22011-07-09 05:47:46 +0000188 // Find feature in table.
189 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000190 Find(StripFlag(Feature), FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000191 // If there is a match
192 if (FeatureEntry) {
193 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
194 Bits &= ~FeatureEntry->Value;
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000195
Evan Cheng91111d22011-07-09 05:47:46 +0000196 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000197 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000198 } else {
199 Bits |= FeatureEntry->Value;
200
201 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000202 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000203 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000204 } else {
205 errs() << "'" << Feature
206 << "' is not a recognized feature for this target"
207 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000208 }
209
210 return Bits;
211}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000212
Evan Cheng91111d22011-07-09 05:47:46 +0000213
Evan Chengfe6e4052011-06-30 01:53:36 +0000214/// getFeatureBits - Get feature bits a CPU.
Jim Laskeydb4621a2005-10-25 15:15:28 +0000215///
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000216uint64_t
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000217SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000218 ArrayRef<SubtargetFeatureKV> CPUTable,
219 ArrayRef<SubtargetFeatureKV> FeatureTable) {
220
221 if (CPUTable.empty() || FeatureTable.empty())
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000222 return 0;
Evan Cheng1a72add62011-07-07 07:07:08 +0000223
Jim Laskey3fee6a52005-09-01 21:36:18 +0000224#ifndef NDEBUG
Eric Christopherdc5072d2014-05-06 20:23:04 +0000225 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000226 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
227 "CPU table is not sorted");
228 }
Eric Christopherdc5072d2014-05-06 20:23:04 +0000229 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
Jim Laskey3fee6a52005-09-01 21:36:18 +0000230 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
231 "CPU features table is not sorted");
232 }
233#endif
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000234 uint64_t Bits = 0; // Resulting bits
Chris Lattner8ff9df22005-10-23 22:23:13 +0000235
Jim Laskeydb4621a2005-10-25 15:15:28 +0000236 // Check if help is needed
Evan Chengfe6e4052011-06-30 01:53:36 +0000237 if (CPU == "help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000238 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000239
Evan Cheng1a72add62011-07-07 07:07:08 +0000240 // Find CPU entry if CPU name is specified.
Eric Christophera9f3a5c2014-05-06 16:29:50 +0000241 else if (!CPU.empty()) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000242 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
243
Evan Cheng1a72add62011-07-07 07:07:08 +0000244 // If there is a match
245 if (CPUEntry) {
246 // Set base feature bits
247 Bits = CPUEntry->Value;
Bill Wendlingf4134192007-06-27 23:34:06 +0000248
Evan Cheng1a72add62011-07-07 07:07:08 +0000249 // Set the feature implied by this CPU feature, if any.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000250 for (auto &FE : FeatureTable) {
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000251 if (CPUEntry->Value & FE.Value)
Eric Christopherdc5072d2014-05-06 20:23:04 +0000252 SetImpliedBits(Bits, &FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000253 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000254 } else {
255 errs() << "'" << CPU
256 << "' is not a recognized processor for this target"
257 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000258 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000259 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000260
Jim Laskey3fee6a52005-09-01 21:36:18 +0000261 // Iterate through each feature
Eric Christopherdc5072d2014-05-06 20:23:04 +0000262 for (auto &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000263 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000264 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000265 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000266
Jim Laskey3fee6a52005-09-01 21:36:18 +0000267 // Find feature in table.
268 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000269 Find(StripFlag(Feature), FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000270 // If there is a match
271 if (FeatureEntry) {
272 // Enable/disable feature in bits
Bill Wendlinge6182262007-05-04 20:38:40 +0000273 if (isEnabled(Feature)) {
274 Bits |= FeatureEntry->Value;
275
276 // For each feature that this implies, set it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000277 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000278 } else {
279 Bits &= ~FeatureEntry->Value;
280
281 // For each feature that implies this, clear it.
Eric Christopherdc5072d2014-05-06 20:23:04 +0000282 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000283 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000284 } else {
285 errs() << "'" << Feature
286 << "' is not a recognized feature for this target"
287 << " (ignoring feature)\n";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000288 }
289 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000290
Jim Laskey3fee6a52005-09-01 21:36:18 +0000291 return Bits;
292}
293
Jim Laskeydb4621a2005-10-25 15:15:28 +0000294/// print - Print feature string.
295///
Chris Lattnerf31ef092009-08-23 21:41:43 +0000296void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000297 for (auto &F : Features)
298 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000299 OS << "\n";
300}
301
Manman Ren49d684e2012-09-12 05:06:18 +0000302#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jim Laskeydb4621a2005-10-25 15:15:28 +0000303/// dump - Dump feature info.
304///
Jim Laskey3fee6a52005-09-01 21:36:18 +0000305void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000306 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000307}
Manman Renc3366cc2012-09-06 19:55:56 +0000308#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000309
Rafael Espindola41d630f2013-10-07 13:34:05 +0000310/// Adds the default features for the specified target triple.
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000311///
312/// FIXME: This is an inelegant way of specifying the features of a
313/// subtarget. It would be better if we could encode this information
314/// into the IR. See <rdar://5972456>.
315///
Evan Chengfe6e4052011-06-30 01:53:36 +0000316void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendling508f6612010-05-11 20:46:04 +0000317 if (Triple.getVendor() == Triple::Apple) {
318 if (Triple.getArch() == Triple::ppc) {
319 // powerpc-apple-*
320 AddFeature("altivec");
321 } else if (Triple.getArch() == Triple::ppc64) {
322 // powerpc64-apple-*
323 AddFeature("64bit");
324 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000325 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000326 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000327}