1 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SubtargetFeature interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/SubtargetFeature.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
24 //===----------------------------------------------------------------------===//
25 // Static Helper Functions
26 //===----------------------------------------------------------------------===//
28 /// hasFlag - Determine if a feature has a flag; '+' or '-'
30 static inline bool hasFlag(StringRef Feature) {
31 assert(!Feature.empty() && "Empty string");
32 // Get first character
34 // Check if first character is '+' or '-' flag
35 return Ch == '+' || Ch =='-';
38 /// StripFlag - Return string stripped of flag.
40 static inline std::string StripFlag(StringRef Feature) {
41 return hasFlag(Feature) ? Feature.substr(1) : Feature;
44 /// isEnabled - Return true if enable flag; '+'.
46 static inline bool isEnabled(StringRef Feature) {
47 assert(!Feature.empty() && "Empty string");
48 // Get first character
50 // Check if first character is '+' for enabled
54 /// Split - Splits a string of comma separated items in to a vector of strings.
56 static void Split(std::vector<std::string> &V, StringRef S) {
57 SmallVector<StringRef, 3> Tmp;
58 S.split(Tmp, ",", -1, false /* KeepEmpty */);
59 V.assign(Tmp.begin(), Tmp.end());
62 /// Join a vector of strings to a string with a comma separating each element.
64 static std::string Join(const std::vector<std::string> &V) {
65 // Start with empty string.
67 // If the vector is not empty
69 // Start with the first feature
71 // For each successive feature
72 for (size_t i = 1; i < V.size(); i++) {
79 // Return the features string
84 void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
85 // Don't add empty features.
87 // Convert to lowercase, prepend flag if we don't already have a flag.
88 Features.push_back(hasFlag(String) ? String.lower()
89 : (Enable ? "+" : "-") + String.lower());
92 /// Find KV in array using binary search.
93 static const SubtargetFeatureKV *Find(StringRef S,
94 ArrayRef<SubtargetFeatureKV> A) {
95 // Binary search the array
96 auto F = std::lower_bound(A.begin(), A.end(), S);
97 // If not found then return NULL
98 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
99 // Return the found array item
103 /// getLongestEntryLength - Return the length of the longest entry in the table.
105 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
107 for (auto &I : Table)
108 MaxLen = std::max(MaxLen, std::strlen(I.Key));
112 /// Display help for feature choices.
114 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
115 ArrayRef<SubtargetFeatureKV> FeatTable) {
116 // Determine the length of the longest CPU and Feature entries.
117 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
118 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
120 // Print the CPU table.
121 errs() << "Available CPUs for this target:\n\n";
122 for (auto &CPU : CPUTable)
123 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
126 // Print the Feature table.
127 errs() << "Available features for this target:\n\n";
128 for (auto &Feature : FeatTable)
129 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
132 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
133 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
136 //===----------------------------------------------------------------------===//
137 // SubtargetFeatures Implementation
138 //===----------------------------------------------------------------------===//
140 SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
141 // Break up string into separate features
142 Split(Features, Initial);
146 std::string SubtargetFeatures::getString() const {
147 return Join(Features);
150 /// SetImpliedBits - For each feature that is (transitively) implied by this
154 void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry,
155 ArrayRef<SubtargetFeatureKV> FeatureTable) {
156 for (auto &FE : FeatureTable) {
157 if (FeatureEntry->Value == FE.Value) continue;
159 if ((FeatureEntry->Implies & FE.Value).any()) {
161 SetImpliedBits(Bits, &FE, FeatureTable);
166 /// ClearImpliedBits - For each feature that (transitively) implies this
167 /// feature, clear it.
170 void ClearImpliedBits(FeatureBitset &Bits,
171 const SubtargetFeatureKV *FeatureEntry,
172 ArrayRef<SubtargetFeatureKV> FeatureTable) {
173 for (auto &FE : FeatureTable) {
174 if (FeatureEntry->Value == FE.Value) continue;
176 if ((FE.Implies & FeatureEntry->Value).any()) {
178 ClearImpliedBits(Bits, &FE, FeatureTable);
183 /// ToggleFeature - Toggle a feature and returns the newly updated feature
186 SubtargetFeatures::ToggleFeature(FeatureBitset Bits, StringRef Feature,
187 ArrayRef<SubtargetFeatureKV> FeatureTable) {
189 // Find feature in table.
190 const SubtargetFeatureKV *FeatureEntry =
191 Find(StripFlag(Feature), FeatureTable);
192 // If there is a match
194 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
195 Bits &= ~FeatureEntry->Value;
196 // For each feature that implies this, clear it.
197 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
199 Bits |= FeatureEntry->Value;
201 // For each feature that this implies, set it.
202 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
205 errs() << "'" << Feature
206 << "' is not a recognized feature for this target"
207 << " (ignoring feature)\n";
214 /// getFeatureBits - Get feature bits a CPU.
217 SubtargetFeatures::getFeatureBits(StringRef CPU,
218 ArrayRef<SubtargetFeatureKV> CPUTable,
219 ArrayRef<SubtargetFeatureKV> FeatureTable) {
221 if (CPUTable.empty() || FeatureTable.empty())
222 return FeatureBitset();
225 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
226 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
227 "CPU table is not sorted");
229 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
230 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
231 "CPU features table is not sorted");
237 // Check if help is needed
239 Help(CPUTable, FeatureTable);
241 // Find CPU entry if CPU name is specified.
242 else if (!CPU.empty()) {
243 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
245 // If there is a match
247 // Set base feature bits
248 Bits = CPUEntry->Value;
250 // Set the feature implied by this CPU feature, if any.
251 for (auto &FE : FeatureTable) {
252 if ((CPUEntry->Value & FE.Value).any())
253 SetImpliedBits(Bits, &FE, FeatureTable);
257 << "' is not a recognized processor for this target"
258 << " (ignoring processor)\n";
262 // Iterate through each feature
263 for (auto &Feature : Features) {
265 if (Feature == "+help")
266 Help(CPUTable, FeatureTable);
268 // Find feature in table.
269 const SubtargetFeatureKV *FeatureEntry =
270 Find(StripFlag(Feature), FeatureTable);
271 // If there is a match
273 // Enable/disable feature in bits
274 if (isEnabled(Feature)) {
275 Bits |= FeatureEntry->Value;
277 // For each feature that this implies, set it.
278 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
280 Bits &= ~FeatureEntry->Value;
282 // For each feature that implies this, clear it.
283 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
286 errs() << "'" << Feature
287 << "' is not a recognized feature for this target"
288 << " (ignoring feature)\n";
295 /// print - Print feature string.
297 void SubtargetFeatures::print(raw_ostream &OS) const {
298 for (auto &F : Features)
303 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
304 /// dump - Dump feature info.
306 void SubtargetFeatures::dump() const {
311 /// Adds the default features for the specified target triple.
313 /// FIXME: This is an inelegant way of specifying the features of a
314 /// subtarget. It would be better if we could encode this information
315 /// into the IR. See <rdar://5972456>.
317 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
318 if (Triple.getVendor() == Triple::Apple) {
319 if (Triple.getArch() == Triple::ppc) {
321 AddFeature("altivec");
322 } else if (Triple.getArch() == Triple::ppc64) {
325 AddFeature("altivec");