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) {
85 // Don't add empty features or features we already have.
87 // Convert to lowercase, prepend flag if we don't already have a flag.
88 Features.push_back(hasFlag(String) ? String.str() : "+" + String.lower());
91 /// Find KV in array using binary search.
92 static const SubtargetFeatureKV *Find(StringRef S,
93 ArrayRef<SubtargetFeatureKV> A) {
94 // Binary search the array
95 auto F = std::lower_bound(A.begin(), A.end(), S);
96 // If not found then return NULL
97 if (F == A.end() || StringRef(F->Key) != S) return nullptr;
98 // Return the found array item
102 /// getLongestEntryLength - Return the length of the longest entry in the table.
104 static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
106 for (auto &I : Table)
107 MaxLen = std::max(MaxLen, std::strlen(I.Key));
111 /// Display help for feature choices.
113 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
114 ArrayRef<SubtargetFeatureKV> FeatTable) {
115 // Determine the length of the longest CPU and Feature entries.
116 unsigned MaxCPULen = getLongestEntryLength(CPUTable);
117 unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
119 // Print the CPU table.
120 errs() << "Available CPUs for this target:\n\n";
121 for (auto &CPU : CPUTable)
122 errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
125 // Print the Feature table.
126 errs() << "Available features for this target:\n\n";
127 for (auto &Feature : FeatTable)
128 errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
131 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
132 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
135 //===----------------------------------------------------------------------===//
136 // SubtargetFeatures Implementation
137 //===----------------------------------------------------------------------===//
139 SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
140 // Break up string into separate features
141 Split(Features, Initial);
145 std::string SubtargetFeatures::getString() const {
146 return Join(Features);
149 /// SetImpliedBits - For each feature that is (transitively) implied by this
153 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
154 ArrayRef<SubtargetFeatureKV> FeatureTable) {
155 for (auto &FE : FeatureTable) {
156 if (FeatureEntry->Value == FE.Value) continue;
158 if (FeatureEntry->Implies & FE.Value) {
160 SetImpliedBits(Bits, &FE, FeatureTable);
165 /// ClearImpliedBits - For each feature that (transitively) implies this
166 /// feature, clear it.
169 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
170 ArrayRef<SubtargetFeatureKV> FeatureTable) {
171 for (auto &FE : FeatureTable) {
172 if (FeatureEntry->Value == FE.Value) continue;
174 if (FE.Implies & FeatureEntry->Value) {
176 ClearImpliedBits(Bits, &FE, FeatureTable);
181 /// ToggleFeature - Toggle a feature and returns the newly updated feature
184 SubtargetFeatures::ToggleFeature(uint64_t Bits, StringRef Feature,
185 ArrayRef<SubtargetFeatureKV> FeatureTable) {
187 // Find feature in table.
188 const SubtargetFeatureKV *FeatureEntry =
189 Find(StripFlag(Feature), FeatureTable);
190 // If there is a match
192 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
193 Bits &= ~FeatureEntry->Value;
195 // For each feature that implies this, clear it.
196 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
198 Bits |= FeatureEntry->Value;
200 // For each feature that this implies, set it.
201 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
205 // Silence this warning for now
207 errs() << "'" << Feature
208 << "' is not a recognized feature for this target"
209 << " (ignoring feature)\n";
217 /// getFeatureBits - Get feature bits a CPU.
220 SubtargetFeatures::getFeatureBits(StringRef CPU,
221 ArrayRef<SubtargetFeatureKV> CPUTable,
222 ArrayRef<SubtargetFeatureKV> FeatureTable) {
224 if (CPUTable.empty() || FeatureTable.empty())
228 for (size_t i = 1, e = CPUTable.size(); i != e; ++i) {
229 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
230 "CPU table is not sorted");
232 for (size_t i = 1, e = FeatureTable.size(); i != e; ++i) {
233 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
234 "CPU features table is not sorted");
237 uint64_t Bits = 0; // Resulting bits
239 // Check if help is needed
241 Help(CPUTable, FeatureTable);
243 // Find CPU entry if CPU name is specified.
244 else if (!CPU.empty()) {
245 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
247 // If there is a match
249 // Set base feature bits
250 Bits = CPUEntry->Value;
252 // Set the feature implied by this CPU feature, if any.
253 for (auto &FE : FeatureTable) {
254 if (CPUEntry->Value & FE.Value)
255 SetImpliedBits(Bits, &FE, FeatureTable);
259 << "' is not a recognized processor for this target"
260 << " (ignoring processor)\n";
264 // Iterate through each feature
265 for (auto &Feature : Features) {
267 if (Feature == "+help")
268 Help(CPUTable, FeatureTable);
270 // Find feature in table.
271 const SubtargetFeatureKV *FeatureEntry =
272 Find(StripFlag(Feature), FeatureTable);
273 // If there is a match
275 // Enable/disable feature in bits
276 if (isEnabled(Feature)) {
277 Bits |= FeatureEntry->Value;
279 // For each feature that this implies, set it.
280 SetImpliedBits(Bits, FeatureEntry, FeatureTable);
282 Bits &= ~FeatureEntry->Value;
284 // For each feature that implies this, clear it.
285 ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
289 // Silence this warning for now
291 errs() << "'" << Feature
292 << "' is not a recognized feature for this target"
293 << " (ignoring feature)\n";
301 /// print - Print feature string.
303 void SubtargetFeatures::print(raw_ostream &OS) const {
304 for (auto &F : Features)
309 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
310 /// dump - Dump feature info.
312 void SubtargetFeatures::dump() const {
317 /// Adds the default features for the specified target triple.
319 /// FIXME: This is an inelegant way of specifying the features of a
320 /// subtarget. It would be better if we could encode this information
321 /// into the IR. See <rdar://5972456>.
323 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
324 if (Triple.getVendor() == Triple::Apple) {
325 if (Triple.getArch() == Triple::ppc) {
327 AddFeature("altivec");
328 } else if (Triple.getArch() == Triple::ppc64) {
331 AddFeature("altivec");