blob: 51aaa4b0aa2593b20037beda8c34fa37e38cea1f [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//
Matthias Braun05e5fd62017-02-21 01:27:29 +000010/// \file Implements the SubtargetFeature interface.
Jim Laskey3fee6a52005-09-01 21:36:18 +000011//
12//===----------------------------------------------------------------------===//
13
Mehdi Aminib550cb12016-04-18 09:17:29 +000014#include "llvm/ADT/ArrayRef.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000015#include "llvm/ADT/SmallVector.h"
Benjamin Kramer5377ad62015-05-28 11:45:32 +000016#include "llvm/ADT/StringExtras.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/Triple.h"
19#include "llvm/MC/SubtargetFeature.h"
20#include "llvm/Support/Compiler.h"
David Greene24328b92010-01-05 01:29:36 +000021#include "llvm/Support/Debug.h"
Benjamin Kramercc863b22011-10-16 16:30:34 +000022#include "llvm/Support/Format.h"
Chris Lattnerf31ef092009-08-23 21:41:43 +000023#include "llvm/Support/raw_ostream.h"
Jim Laskey3fee6a52005-09-01 21:36:18 +000024#include <algorithm>
Jim Laskey3fee6a52005-09-01 21:36:18 +000025#include <cassert>
Eugene Zelenko44d95122017-02-09 01:09:54 +000026#include <cstddef>
27#include <cstring>
28#include <iterator>
29#include <string>
30#include <vector>
31
Jim Laskey3fee6a52005-09-01 21:36:18 +000032using namespace llvm;
33
Matthias Braun05e5fd62017-02-21 01:27:29 +000034/// Determine if a feature has a flag; '+' or '-'
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000035static inline bool hasFlag(StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000036 assert(!Feature.empty() && "Empty string");
37 // Get first character
38 char Ch = Feature[0];
39 // Check if first character is '+' or '-' flag
40 return Ch == '+' || Ch =='-';
41}
42
Matthias Braun05e5fd62017-02-21 01:27:29 +000043/// Return string stripped of flag.
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000044static inline std::string StripFlag(StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000045 return hasFlag(Feature) ? Feature.substr(1) : Feature;
46}
47
Matthias Braun05e5fd62017-02-21 01:27:29 +000048/// Return true if enable flag; '+'.
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000049static inline bool isEnabled(StringRef Feature) {
Chris Lattner18a70c32005-10-23 05:26:26 +000050 assert(!Feature.empty() && "Empty string");
51 // Get first character
52 char Ch = Feature[0];
53 // Check if first character is '+' for enabled
54 return Ch == '+';
55}
56
Matthias Braun05e5fd62017-02-21 01:27:29 +000057/// Splits a string of comma separated items in to a vector of strings.
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
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
Matthias Braun05e5fd62017-02-21 01:27:29 +000083/// Return the length of the longest entry in the table.
Eric Christopherdc5072d2014-05-06 20:23:04 +000084static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
Chris Lattner8ff9df22005-10-23 22:23:13 +000085 size_t MaxLen = 0;
Eric Christopherdc5072d2014-05-06 20:23:04 +000086 for (auto &I : Table)
87 MaxLen = std::max(MaxLen, std::strlen(I.Key));
Chris Lattner8ff9df22005-10-23 22:23:13 +000088 return MaxLen;
89}
90
Jim Laskey27d628d2005-09-02 19:27:43 +000091/// Display help for feature choices.
Eric Christopherdc5072d2014-05-06 20:23:04 +000092static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
93 ArrayRef<SubtargetFeatureKV> FeatTable) {
Chris Lattner8ff9df22005-10-23 22:23:13 +000094 // Determine the length of the longest CPU and Feature entries.
Eric Christopherdc5072d2014-05-06 20:23:04 +000095 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
96 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
Chris Lattnerf64f8402005-10-23 05:33:39 +000097
Chris Lattner8ff9df22005-10-23 22:23:13 +000098 // Print the CPU table.
Chris Lattnerf31ef092009-08-23 21:41:43 +000099 errs() << "Available CPUs for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000100 for (auto &CPU : CPUTable)
101 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000102 errs() << '\n';
103
Chris Lattner8ff9df22005-10-23 22:23:13 +0000104 // Print the Feature table.
Chris Lattnerf31ef092009-08-23 21:41:43 +0000105 errs() << "Available features for this target:\n\n";
Eric Christopherdc5072d2014-05-06 20:23:04 +0000106 for (auto &Feature : FeatTable)
107 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
Benjamin Kramercc863b22011-10-16 16:30:34 +0000108 errs() << '\n';
109
Chris Lattnerf31ef092009-08-23 21:41:43 +0000110 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramercc863b22011-10-16 16:30:34 +0000111 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey27d628d2005-09-02 19:27:43 +0000112}
113
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000114SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
Chris Lattner18a70c32005-10-23 05:26:26 +0000115 // Break up string into separate features
116 Split(Features, Initial);
117}
118
Rafael Espindola968af4f2011-07-01 04:40:50 +0000119std::string SubtargetFeatures::getString() const {
Benjamin Kramer5377ad62015-05-28 11:45:32 +0000120 return join(Features.begin(), Features.end(), ",");
Chris Lattner18a70c32005-10-23 05:26:26 +0000121}
Chris Lattner18a70c32005-10-23 05:26:26 +0000122
Matthias Braun05e5fd62017-02-21 01:27:29 +0000123/// For each feature that is (transitively) implied by this feature, set it.
Bill Wendlinge6182262007-05-04 20:38:40 +0000124static
Matthias Braun05e5fd62017-02-21 01:27:29 +0000125void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV &FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000126 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Matthias Braun05e5fd62017-02-21 01:27:29 +0000127 for (const SubtargetFeatureKV &FE : FeatureTable) {
128 if (FeatureEntry.Value == FE.Value) continue;
Bill Wendlinge6182262007-05-04 20:38:40 +0000129
Matthias Braun05e5fd62017-02-21 01:27:29 +0000130 if ((FeatureEntry.Implies & FE.Value).any()) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000131 Bits |= FE.Value;
Matthias Braun05e5fd62017-02-21 01:27:29 +0000132 SetImpliedBits(Bits, FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000133 }
134 }
135}
136
Matthias Braun05e5fd62017-02-21 01:27:29 +0000137/// For each feature that (transitively) implies this feature, clear it.
Bill Wendlinge6182262007-05-04 20:38:40 +0000138static
Matthias Braun05e5fd62017-02-21 01:27:29 +0000139void ClearImpliedBits(FeatureBitset &Bits,
140 const SubtargetFeatureKV &FeatureEntry,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000141 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Matthias Braun05e5fd62017-02-21 01:27:29 +0000142 for (const SubtargetFeatureKV &FE : FeatureTable) {
143 if (FeatureEntry.Value == FE.Value) continue;
Bill Wendlinge6182262007-05-04 20:38:40 +0000144
Matthias Braun05e5fd62017-02-21 01:27:29 +0000145 if ((FE.Implies & FeatureEntry.Value).any()) {
Bill Wendlinge6182262007-05-04 20:38:40 +0000146 Bits &= ~FE.Value;
Matthias Braun05e5fd62017-02-21 01:27:29 +0000147 ClearImpliedBits(Bits, FE, FeatureTable);
Bill Wendlinge6182262007-05-04 20:38:40 +0000148 }
149 }
150}
Jim Laskeydb4621a2005-10-25 15:15:28 +0000151
Artyom Skrobov8c699232016-01-05 10:25:56 +0000152void
153SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
Eric Christopher0e6f41c2014-05-06 21:04:27 +0000154 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Evan Cheng91111d22011-07-09 05:47:46 +0000155 // Find feature in table.
156 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherdc5072d2014-05-06 20:23:04 +0000157 Find(StripFlag(Feature), FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000158 // If there is a match
159 if (FeatureEntry) {
160 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
161 Bits &= ~FeatureEntry->Value;
Evan Cheng91111d22011-07-09 05:47:46 +0000162 // For each feature that implies this, clear it.
Matthias Braun05e5fd62017-02-21 01:27:29 +0000163 ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000164 } else {
165 Bits |= FeatureEntry->Value;
166
167 // For each feature that this implies, set it.
Matthias Braun05e5fd62017-02-21 01:27:29 +0000168 SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
Evan Cheng91111d22011-07-09 05:47:46 +0000169 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000170 } else {
Matthias Braun05e5fd62017-02-21 01:27:29 +0000171 errs() << "'" << Feature << "' is not a recognized feature for this target"
Artyom Skroboveab75152014-01-25 16:56:18 +0000172 << " (ignoring feature)\n";
Evan Cheng91111d22011-07-09 05:47:46 +0000173 }
Evan Cheng91111d22011-07-09 05:47:46 +0000174}
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000175
Artyom Skrobov8c699232016-01-05 10:25:56 +0000176void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
John Brawnd03d2292015-06-05 13:29:24 +0000177 ArrayRef<SubtargetFeatureKV> FeatureTable) {
John Brawnd03d2292015-06-05 13:29:24 +0000178 assert(hasFlag(Feature));
179
180 // Find feature in table.
181 const SubtargetFeatureKV *FeatureEntry =
182 Find(StripFlag(Feature), FeatureTable);
183 // If there is a match
184 if (FeatureEntry) {
185 // Enable/disable feature in bits
186 if (isEnabled(Feature)) {
Artyom Skrobov8c699232016-01-05 10:25:56 +0000187 Bits |= FeatureEntry->Value;
John Brawnd03d2292015-06-05 13:29:24 +0000188
189 // For each feature that this implies, set it.
Matthias Braun05e5fd62017-02-21 01:27:29 +0000190 SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
John Brawnd03d2292015-06-05 13:29:24 +0000191 } else {
192 Bits &= ~FeatureEntry->Value;
193
194 // For each feature that implies this, clear it.
Matthias Braun05e5fd62017-02-21 01:27:29 +0000195 ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
John Brawnd03d2292015-06-05 13:29:24 +0000196 }
197 } else {
Matthias Braun05e5fd62017-02-21 01:27:29 +0000198 errs() << "'" << Feature << "' is not a recognized feature for this target"
John Brawnd03d2292015-06-05 13:29:24 +0000199 << " (ignoring feature)\n";
200 }
John Brawnd03d2292015-06-05 13:29:24 +0000201}
202
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000203FeatureBitset
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000204SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherdc5072d2014-05-06 20:23:04 +0000205 ArrayRef<SubtargetFeatureKV> CPUTable,
206 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Eric Christopherdc5072d2014-05-06 20:23:04 +0000207 if (CPUTable.empty() || FeatureTable.empty())
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000208 return FeatureBitset();
Evan Cheng1a72add62011-07-07 07:07:08 +0000209
Craig Topper5f167722016-01-03 07:33:45 +0000210 assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
211 "CPU table is not sorted");
212 assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
213 "CPU features table is not sorted");
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())
Matthias Braun05e5fd62017-02-21 01:27:29 +0000233 SetImpliedBits(Bits, FE, FeatureTable);
Evan Cheng1a72add62011-07-07 07:07:08 +0000234 }
Artyom Skroboveab75152014-01-25 16:56:18 +0000235 } else {
Matthias Braun05e5fd62017-02-21 01:27:29 +0000236 errs() << "'" << CPU << "' is not a recognized processor for this target"
Artyom Skroboveab75152014-01-25 16:56:18 +0000237 << " (ignoring processor)\n";
Bill Wendlingf4134192007-06-27 23:34:06 +0000238 }
Jim Laskey3fee6a52005-09-01 21:36:18 +0000239 }
Evan Cheng1a72add62011-07-07 07:07:08 +0000240
Jim Laskey3fee6a52005-09-01 21:36:18 +0000241 // Iterate through each feature
Matthias Braun05e5fd62017-02-21 01:27:29 +0000242 for (const std::string &Feature : Features) {
Jim Laskey27d628d2005-09-02 19:27:43 +0000243 // Check for help
Chris Lattner8ff9df22005-10-23 22:23:13 +0000244 if (Feature == "+help")
Eric Christopherdc5072d2014-05-06 20:23:04 +0000245 Help(CPUTable, FeatureTable);
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000246
Artyom Skrobov8c699232016-01-05 10:25:56 +0000247 ApplyFeatureFlag(Bits, Feature, FeatureTable);
Jim Laskey3fee6a52005-09-01 21:36:18 +0000248 }
Bill Wendlinge6182262007-05-04 20:38:40 +0000249
Jim Laskey3fee6a52005-09-01 21:36:18 +0000250 return Bits;
251}
252
Chris Lattnerf31ef092009-08-23 21:41:43 +0000253void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher9c928472014-05-06 21:20:29 +0000254 for (auto &F : Features)
255 OS << F << " ";
Jim Laskey3fee6a52005-09-01 21:36:18 +0000256 OS << "\n";
257}
258
Matthias Braun8c209aa2017-01-28 02:02:38 +0000259#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000260LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
David Greene24328b92010-01-05 01:29:36 +0000261 print(dbgs());
Jim Laskey3fee6a52005-09-01 21:36:18 +0000262}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000263#endif
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000264
Evan Chengfe6e4052011-06-30 01:53:36 +0000265void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Matthias Braun05e5fd62017-02-21 01:27:29 +0000266 // FIXME: This is an inelegant way of specifying the features of a
267 // subtarget. It would be better if we could encode this information
268 // into the IR. See <rdar://5972456>.
Bill Wendling508f6612010-05-11 20:46:04 +0000269 if (Triple.getVendor() == Triple::Apple) {
270 if (Triple.getArch() == Triple::ppc) {
271 // powerpc-apple-*
272 AddFeature("altivec");
273 } else if (Triple.getArch() == Triple::ppc64) {
274 // powerpc64-apple-*
275 AddFeature("64bit");
276 AddFeature("altivec");
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000277 }
Bill Wendlinga12c1ff2010-05-11 00:30:02 +0000278 }
Viktor Kutuzovc3e2b6b2009-11-18 20:20:05 +0000279}