blob: b69af24b531e699f1585b959033d825820eb8c21 [file] [log] [blame]
Jim Laskeyb3302db2005-09-01 21:36:18 +00001//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Laskeyb3302db2005-09-01 21:36:18 +00007//
8//===----------------------------------------------------------------------===//
9//
Matthias Braun60202ab2017-02-21 01:27:29 +000010/// \file Implements the SubtargetFeature interface.
Jim Laskeyb3302db2005-09-01 21:36:18 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthe3e43d92017-06-06 11:49:48 +000014#include "llvm/MC/SubtargetFeature.h"
Mehdi Aminif6071e12016-04-18 09:17:29 +000015#include "llvm/ADT/ArrayRef.h"
Eugene Zelenko3a124c02017-02-09 01:09:54 +000016#include "llvm/ADT/SmallVector.h"
Benjamin Kramer7f4bf082015-05-28 11:45:32 +000017#include "llvm/ADT/StringExtras.h"
Eugene Zelenko3a124c02017-02-09 01:09:54 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Triple.h"
Nico Weber0f38c602018-04-30 14:59:11 +000020#include "llvm/Config/llvm-config.h"
Eugene Zelenko3a124c02017-02-09 01:09:54 +000021#include "llvm/Support/Compiler.h"
David Greene9759c302010-01-05 01:29:36 +000022#include "llvm/Support/Debug.h"
Benjamin Kramer962bad72011-10-16 16:30:34 +000023#include "llvm/Support/Format.h"
Chris Lattnere0c86af2009-08-23 21:41:43 +000024#include "llvm/Support/raw_ostream.h"
Jim Laskeyb3302db2005-09-01 21:36:18 +000025#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000026#include <cassert>
Eugene Zelenko3a124c02017-02-09 01:09:54 +000027#include <cstddef>
28#include <cstring>
29#include <iterator>
30#include <string>
31#include <vector>
32
Jim Laskeyb3302db2005-09-01 21:36:18 +000033using namespace llvm;
34
Matthias Braun60202ab2017-02-21 01:27:29 +000035/// Determine if a feature has a flag; '+' or '-'
Craig Topper24ae56d2014-08-30 16:48:02 +000036static inline bool hasFlag(StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000037 assert(!Feature.empty() && "Empty string");
38 // Get first character
39 char Ch = Feature[0];
40 // Check if first character is '+' or '-' flag
41 return Ch == '+' || Ch =='-';
42}
43
Matthias Braun60202ab2017-02-21 01:27:29 +000044/// Return string stripped of flag.
Craig Topper24ae56d2014-08-30 16:48:02 +000045static inline std::string StripFlag(StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000046 return hasFlag(Feature) ? Feature.substr(1) : Feature;
47}
48
Matthias Braun60202ab2017-02-21 01:27:29 +000049/// Return true if enable flag; '+'.
Craig Topper24ae56d2014-08-30 16:48:02 +000050static inline bool isEnabled(StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000051 assert(!Feature.empty() && "Empty string");
52 // Get first character
53 char Ch = Feature[0];
54 // Check if first character is '+' for enabled
55 return Ch == '+';
56}
57
Matthias Braun60202ab2017-02-21 01:27:29 +000058/// Splits a string of comma separated items in to a vector of strings.
Craig Topper24ae56d2014-08-30 16:48:02 +000059static void Split(std::vector<std::string> &V, StringRef S) {
Hans Wennborg9af09022014-08-11 02:21:32 +000060 SmallVector<StringRef, 3> Tmp;
Chandler Carruth6aaf0a62015-09-10 06:12:31 +000061 S.split(Tmp, ',', -1, false /* KeepEmpty */);
Eric Christophere02c34b2014-05-13 19:55:17 +000062 V.assign(Tmp.begin(), Tmp.end());
Jim Laskeyb3302db2005-09-01 21:36:18 +000063}
64
Craig Topper9a7c0032015-03-31 05:52:57 +000065void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
Craig Topper74381e22015-03-28 03:24:19 +000066 // Don't add empty features.
Eric Christopher5c34f992014-05-06 02:37:26 +000067 if (!String.empty())
68 // Convert to lowercase, prepend flag if we don't already have a flag.
Craig Topper9a7c0032015-03-31 05:52:57 +000069 Features.push_back(hasFlag(String) ? String.lower()
70 : (Enable ? "+" : "-") + String.lower());
Jim Laskeyb3302db2005-09-01 21:36:18 +000071}
72
Jim Laskey34bd5d52005-10-25 15:15:28 +000073/// Find KV in array using binary search.
Eric Christopherd4741812014-05-06 20:23:04 +000074static const SubtargetFeatureKV *Find(StringRef S,
75 ArrayRef<SubtargetFeatureKV> A) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000076 // Binary search the array
Eric Christopherd4741812014-05-06 20:23:04 +000077 auto F = std::lower_bound(A.begin(), A.end(), S);
Jim Laskeyb3302db2005-09-01 21:36:18 +000078 // If not found then return NULL
Eric Christopherd4741812014-05-06 20:23:04 +000079 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
Jim Laskeyb3302db2005-09-01 21:36:18 +000080 // Return the found array item
81 return F;
82}
83
Matthias Braun60202ab2017-02-21 01:27:29 +000084/// Return the length of the longest entry in the table.
Eric Christopherd4741812014-05-06 20:23:04 +000085static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
Chris Lattner3e808a42005-10-23 22:23:13 +000086 size_t MaxLen = 0;
Eric Christopherd4741812014-05-06 20:23:04 +000087 for (auto &I : Table)
88 MaxLen = std::max(MaxLen, std::strlen(I.Key));
Chris Lattner3e808a42005-10-23 22:23:13 +000089 return MaxLen;
90}
91
Jim Laskey839615a2005-09-02 19:27:43 +000092/// Display help for feature choices.
Eric Christopherd4741812014-05-06 20:23:04 +000093static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
94 ArrayRef<SubtargetFeatureKV> FeatTable) {
Chris Lattner3e808a42005-10-23 22:23:13 +000095 // Determine the length of the longest CPU and Feature entries.
Eric Christopherd4741812014-05-06 20:23:04 +000096 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
97 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
Chris Lattnerba76c212005-10-23 05:33:39 +000098
Chris Lattner3e808a42005-10-23 22:23:13 +000099 // Print the CPU table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000100 errs() << "Available CPUs for this target:\n\n";
Eric Christopherd4741812014-05-06 20:23:04 +0000101 for (auto &CPU : CPUTable)
102 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
Benjamin Kramer962bad72011-10-16 16:30:34 +0000103 errs() << '\n';
104
Chris Lattner3e808a42005-10-23 22:23:13 +0000105 // Print the Feature table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000106 errs() << "Available features for this target:\n\n";
Eric Christopherd4741812014-05-06 20:23:04 +0000107 for (auto &Feature : FeatTable)
108 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
Benjamin Kramer962bad72011-10-16 16:30:34 +0000109 errs() << '\n';
110
Chris Lattnere0c86af2009-08-23 21:41:43 +0000111 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Benjamin Kramer962bad72011-10-16 16:30:34 +0000112 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Jim Laskey839615a2005-09-02 19:27:43 +0000113}
114
Craig Topper24ae56d2014-08-30 16:48:02 +0000115SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
Chris Lattner54195c12005-10-23 05:26:26 +0000116 // Break up string into separate features
117 Split(Features, Initial);
118}
119
Rafael Espindola3f9b9eb2011-07-01 04:40:50 +0000120std::string SubtargetFeatures::getString() const {
Benjamin Kramer7f4bf082015-05-28 11:45:32 +0000121 return join(Features.begin(), Features.end(), ",");
Chris Lattner54195c12005-10-23 05:26:26 +0000122}
Chris Lattner54195c12005-10-23 05:26:26 +0000123
Matthias Braun60202ab2017-02-21 01:27:29 +0000124/// For each feature that is (transitively) implied by this feature, set it.
Bill Wendling4222d802007-05-04 20:38:40 +0000125static
Matthias Braun60202ab2017-02-21 01:27:29 +0000126void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV &FeatureEntry,
Eric Christopherd4741812014-05-06 20:23:04 +0000127 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Matthias Braun60202ab2017-02-21 01:27:29 +0000128 for (const SubtargetFeatureKV &FE : FeatureTable) {
129 if (FeatureEntry.Value == FE.Value) continue;
Bill Wendling4222d802007-05-04 20:38:40 +0000130
Matthias Braun60202ab2017-02-21 01:27:29 +0000131 if ((FeatureEntry.Implies & FE.Value).any()) {
Bill Wendling4222d802007-05-04 20:38:40 +0000132 Bits |= FE.Value;
Matthias Braun60202ab2017-02-21 01:27:29 +0000133 SetImpliedBits(Bits, FE, FeatureTable);
Bill Wendling4222d802007-05-04 20:38:40 +0000134 }
135 }
136}
137
Matthias Braun60202ab2017-02-21 01:27:29 +0000138/// For each feature that (transitively) implies this feature, clear it.
Bill Wendling4222d802007-05-04 20:38:40 +0000139static
Matthias Braun60202ab2017-02-21 01:27:29 +0000140void ClearImpliedBits(FeatureBitset &Bits,
141 const SubtargetFeatureKV &FeatureEntry,
Eric Christopherd4741812014-05-06 20:23:04 +0000142 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Matthias Braun60202ab2017-02-21 01:27:29 +0000143 for (const SubtargetFeatureKV &FE : FeatureTable) {
144 if (FeatureEntry.Value == FE.Value) continue;
Bill Wendling4222d802007-05-04 20:38:40 +0000145
Matthias Braun60202ab2017-02-21 01:27:29 +0000146 if ((FE.Implies & FeatureEntry.Value).any()) {
Bill Wendling4222d802007-05-04 20:38:40 +0000147 Bits &= ~FE.Value;
Matthias Braun60202ab2017-02-21 01:27:29 +0000148 ClearImpliedBits(Bits, FE, FeatureTable);
Bill Wendling4222d802007-05-04 20:38:40 +0000149 }
150 }
151}
Jim Laskey34bd5d52005-10-25 15:15:28 +0000152
Artyom Skrobov176a9b2a2016-01-05 10:25:56 +0000153void
154SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
Eric Christopherd2ba53b2014-05-06 21:04:27 +0000155 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Evan Chengffc0e732011-07-09 05:47:46 +0000156 // Find feature in table.
157 const SubtargetFeatureKV *FeatureEntry =
Eric Christopherd4741812014-05-06 20:23:04 +0000158 Find(StripFlag(Feature), FeatureTable);
Evan Chengffc0e732011-07-09 05:47:46 +0000159 // If there is a match
160 if (FeatureEntry) {
161 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
162 Bits &= ~FeatureEntry->Value;
Evan Chengffc0e732011-07-09 05:47:46 +0000163 // For each feature that implies this, clear it.
Matthias Braun60202ab2017-02-21 01:27:29 +0000164 ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
Evan Chengffc0e732011-07-09 05:47:46 +0000165 } else {
166 Bits |= FeatureEntry->Value;
167
168 // For each feature that this implies, set it.
Matthias Braun60202ab2017-02-21 01:27:29 +0000169 SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
Evan Chengffc0e732011-07-09 05:47:46 +0000170 }
Artyom Skrobov85ae0342014-01-25 16:56:18 +0000171 } else {
Matthias Braun60202ab2017-02-21 01:27:29 +0000172 errs() << "'" << Feature << "' is not a recognized feature for this target"
Artyom Skrobov85ae0342014-01-25 16:56:18 +0000173 << " (ignoring feature)\n";
Evan Chengffc0e732011-07-09 05:47:46 +0000174 }
Evan Chengffc0e732011-07-09 05:47:46 +0000175}
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000176
Artyom Skrobov176a9b2a2016-01-05 10:25:56 +0000177void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
John Brawnc1c9bc12015-06-05 13:29:24 +0000178 ArrayRef<SubtargetFeatureKV> FeatureTable) {
John Brawnc1c9bc12015-06-05 13:29:24 +0000179 assert(hasFlag(Feature));
180
181 // Find feature in table.
182 const SubtargetFeatureKV *FeatureEntry =
183 Find(StripFlag(Feature), FeatureTable);
184 // If there is a match
185 if (FeatureEntry) {
186 // Enable/disable feature in bits
187 if (isEnabled(Feature)) {
Artyom Skrobov176a9b2a2016-01-05 10:25:56 +0000188 Bits |= FeatureEntry->Value;
John Brawnc1c9bc12015-06-05 13:29:24 +0000189
190 // For each feature that this implies, set it.
Matthias Braun60202ab2017-02-21 01:27:29 +0000191 SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
John Brawnc1c9bc12015-06-05 13:29:24 +0000192 } else {
193 Bits &= ~FeatureEntry->Value;
194
195 // For each feature that implies this, clear it.
Matthias Braun60202ab2017-02-21 01:27:29 +0000196 ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
John Brawnc1c9bc12015-06-05 13:29:24 +0000197 }
198 } else {
Matthias Braun60202ab2017-02-21 01:27:29 +0000199 errs() << "'" << Feature << "' is not a recognized feature for this target"
John Brawnc1c9bc12015-06-05 13:29:24 +0000200 << " (ignoring feature)\n";
201 }
John Brawnc1c9bc12015-06-05 13:29:24 +0000202}
203
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000204FeatureBitset
Craig Topper24ae56d2014-08-30 16:48:02 +0000205SubtargetFeatures::getFeatureBits(StringRef CPU,
Eric Christopherd4741812014-05-06 20:23:04 +0000206 ArrayRef<SubtargetFeatureKV> CPUTable,
207 ArrayRef<SubtargetFeatureKV> FeatureTable) {
Eric Christopherd4741812014-05-06 20:23:04 +0000208 if (CPUTable.empty() || FeatureTable.empty())
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000209 return FeatureBitset();
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000210
Craig Topper4d0731f2016-01-03 07:33:45 +0000211 assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
212 "CPU table is not sorted");
213 assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
214 "CPU features table is not sorted");
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000215 // Resulting bits
216 FeatureBitset Bits;
Chris Lattner3e808a42005-10-23 22:23:13 +0000217
Jim Laskey34bd5d52005-10-25 15:15:28 +0000218 // Check if help is needed
Evan Cheng276365d2011-06-30 01:53:36 +0000219 if (CPU == "help")
Eric Christopherd4741812014-05-06 20:23:04 +0000220 Help(CPUTable, FeatureTable);
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000221
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000222 // Find CPU entry if CPU name is specified.
Eric Christopherbfc3f302014-05-06 16:29:50 +0000223 else if (!CPU.empty()) {
Eric Christopherd4741812014-05-06 20:23:04 +0000224 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
225
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000226 // If there is a match
227 if (CPUEntry) {
228 // Set base feature bits
229 Bits = CPUEntry->Value;
Bill Wendling1a636de2007-06-27 23:34:06 +0000230
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000231 // Set the feature implied by this CPU feature, if any.
Eric Christopherd4741812014-05-06 20:23:04 +0000232 for (auto &FE : FeatureTable) {
Michael Kupersteind714fcf2015-05-26 10:47:10 +0000233 if ((CPUEntry->Value & FE.Value).any())
Matthias Braun60202ab2017-02-21 01:27:29 +0000234 SetImpliedBits(Bits, FE, FeatureTable);
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000235 }
Artyom Skrobov85ae0342014-01-25 16:56:18 +0000236 } else {
Matthias Braun60202ab2017-02-21 01:27:29 +0000237 errs() << "'" << CPU << "' is not a recognized processor for this target"
Artyom Skrobov85ae0342014-01-25 16:56:18 +0000238 << " (ignoring processor)\n";
Bill Wendling1a636de2007-06-27 23:34:06 +0000239 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000240 }
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000241
Jim Laskeyb3302db2005-09-01 21:36:18 +0000242 // Iterate through each feature
Matthias Braun60202ab2017-02-21 01:27:29 +0000243 for (const std::string &Feature : Features) {
Jim Laskey839615a2005-09-02 19:27:43 +0000244 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000245 if (Feature == "+help")
Eric Christopherd4741812014-05-06 20:23:04 +0000246 Help(CPUTable, FeatureTable);
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000247
Artyom Skrobov176a9b2a2016-01-05 10:25:56 +0000248 ApplyFeatureFlag(Bits, Feature, FeatureTable);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000249 }
Bill Wendling4222d802007-05-04 20:38:40 +0000250
Jim Laskeyb3302db2005-09-01 21:36:18 +0000251 return Bits;
252}
253
Chris Lattnere0c86af2009-08-23 21:41:43 +0000254void SubtargetFeatures::print(raw_ostream &OS) const {
Eric Christopher887ab992014-05-06 21:20:29 +0000255 for (auto &F : Features)
256 OS << F << " ";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000257 OS << "\n";
258}
259
Aaron Ballman1d03d382017-10-15 14:32:27 +0000260#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000261LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
David Greene9759c302010-01-05 01:29:36 +0000262 print(dbgs());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000263}
Matthias Braun88d20752017-01-28 02:02:38 +0000264#endif
Viktor Kutuzove823db82009-11-18 20:20:05 +0000265
Evan Cheng276365d2011-06-30 01:53:36 +0000266void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Matthias Braun60202ab2017-02-21 01:27:29 +0000267 // FIXME: This is an inelegant way of specifying the features of a
268 // subtarget. It would be better if we could encode this information
269 // into the IR. See <rdar://5972456>.
Bill Wendlingf6d84812010-05-11 20:46:04 +0000270 if (Triple.getVendor() == Triple::Apple) {
271 if (Triple.getArch() == Triple::ppc) {
272 // powerpc-apple-*
273 AddFeature("altivec");
274 } else if (Triple.getArch() == Triple::ppc64) {
275 // powerpc64-apple-*
276 AddFeature("64bit");
277 AddFeature("altivec");
Viktor Kutuzove823db82009-11-18 20:20:05 +0000278 }
Bill Wendling81043ee2010-05-11 00:30:02 +0000279 }
Viktor Kutuzove823db82009-11-18 20:20:05 +0000280}