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(const 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(const StringRef Feature) {
41 return hasFlag(Feature) ? Feature.substr(1) : Feature;
44 /// isEnabled - Return true if enable flag; '+'.
46 static inline bool isEnabled(const StringRef Feature) {
47 assert(!Feature.empty() && "Empty string");
48 // Get first character
50 // Check if first character is '+' for enabled
54 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
56 static inline std::string PrependFlag(const StringRef Feature,
58 assert(!Feature.empty() && "Empty string");
61 std::string Prefix = IsEnabled ? "+" : "-";
66 /// Split - Splits a string of comma separated items in to a vector of strings.
68 static void Split(std::vector<std::string> &V, const StringRef S) {
72 // Start at beginning of string.
75 // Find the next comma
76 size_t Comma = S.find(',', Pos);
77 // If no comma found then the rest of the string is used
78 if (Comma == std::string::npos) {
79 // Add string to vector
80 V.push_back(S.substr(Pos));
83 // Otherwise add substring to vector
84 V.push_back(S.substr(Pos, Comma - Pos));
85 // Advance to next item
90 /// Join a vector of strings to a string with a comma separating each element.
92 static std::string Join(const std::vector<std::string> &V) {
93 // Start with empty string.
95 // If the vector is not empty
97 // Start with the first feature
99 // For each successive feature
100 for (size_t i = 1; i < V.size(); i++) {
107 // Return the features string
112 void SubtargetFeatures::AddFeature(const StringRef String,
114 // Don't add empty features
115 if (!String.empty()) {
116 // Convert to lowercase, prepend flag and add to vector
117 Features.push_back(PrependFlag(String.lower(), IsEnabled));
121 /// Find KV in array using binary search.
122 template<typename T> const T *Find(const StringRef S, const T *A, size_t L) {
123 // Make the lower bound element we're looking for
126 // Determine the end of the array
128 // Binary search the array
129 const T *F = std::lower_bound(A, Hi, KV);
130 // If not found then return NULL
131 if (F == Hi || StringRef(F->Key) != S) return NULL;
132 // Return the found array item
136 /// getLongestEntryLength - Return the length of the longest entry in the table.
138 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
141 for (size_t i = 0; i < Size; i++)
142 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
146 /// Display help for feature choices.
148 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
149 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
150 // Determine the length of the longest CPU and Feature entries.
151 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
152 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
154 // Print the CPU table.
155 errs() << "Available CPUs for this target:\n\n";
156 for (size_t i = 0; i != CPUTableSize; i++)
157 errs() << format(" %-*s - %s.\n",
158 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
161 // Print the Feature table.
162 errs() << "Available features for this target:\n\n";
163 for (size_t i = 0; i != FeatTableSize; i++)
164 errs() << format(" %-*s - %s.\n",
165 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
168 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
169 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
173 //===----------------------------------------------------------------------===//
174 // SubtargetFeatures Implementation
175 //===----------------------------------------------------------------------===//
177 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
178 // Break up string into separate features
179 Split(Features, Initial);
183 std::string SubtargetFeatures::getString() const {
184 return Join(Features);
187 /// SetImpliedBits - For each feature that is (transitively) implied by this
191 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
192 const SubtargetFeatureKV *FeatureTable,
193 size_t FeatureTableSize) {
194 for (size_t i = 0; i < FeatureTableSize; ++i) {
195 const SubtargetFeatureKV &FE = FeatureTable[i];
197 if (FeatureEntry->Value == FE.Value) continue;
199 if (FeatureEntry->Implies & FE.Value) {
201 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
206 /// ClearImpliedBits - For each feature that (transitively) implies this
207 /// feature, clear it.
210 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
211 const SubtargetFeatureKV *FeatureTable,
212 size_t FeatureTableSize) {
213 for (size_t i = 0; i < FeatureTableSize; ++i) {
214 const SubtargetFeatureKV &FE = FeatureTable[i];
216 if (FeatureEntry->Value == FE.Value) continue;
218 if (FE.Implies & FeatureEntry->Value) {
220 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
225 /// ToggleFeature - Toggle a feature and returns the newly updated feature
228 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
229 const SubtargetFeatureKV *FeatureTable,
230 size_t FeatureTableSize) {
231 // Find feature in table.
232 const SubtargetFeatureKV *FeatureEntry =
233 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
234 // If there is a match
236 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
237 Bits &= ~FeatureEntry->Value;
239 // For each feature that implies this, clear it.
240 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
242 Bits |= FeatureEntry->Value;
244 // For each feature that this implies, set it.
245 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
248 errs() << "'" << Feature
249 << "' is not a recognized feature for this target"
250 << " (ignoring feature)\n";
257 /// getFeatureBits - Get feature bits a CPU.
259 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
260 const SubtargetFeatureKV *CPUTable,
262 const SubtargetFeatureKV *FeatureTable,
263 size_t FeatureTableSize) {
264 if (!FeatureTableSize || !CPUTableSize)
268 for (size_t i = 1; i < CPUTableSize; i++) {
269 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
270 "CPU table is not sorted");
272 for (size_t i = 1; i < FeatureTableSize; i++) {
273 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
274 "CPU features table is not sorted");
277 uint64_t Bits = 0; // Resulting bits
279 // Check if help is needed
281 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
283 // Find CPU entry if CPU name is specified.
285 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
286 // If there is a match
288 // Set base feature bits
289 Bits = CPUEntry->Value;
291 // Set the feature implied by this CPU feature, if any.
292 for (size_t i = 0; i < FeatureTableSize; ++i) {
293 const SubtargetFeatureKV &FE = FeatureTable[i];
294 if (CPUEntry->Value & FE.Value)
295 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
299 << "' is not a recognized processor for this target"
300 << " (ignoring processor)\n";
304 // Iterate through each feature
305 for (size_t i = 0, E = Features.size(); i < E; i++) {
306 const StringRef Feature = Features[i];
309 if (Feature == "+help")
310 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
312 // Find feature in table.
313 const SubtargetFeatureKV *FeatureEntry =
314 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
315 // If there is a match
317 // Enable/disable feature in bits
318 if (isEnabled(Feature)) {
319 Bits |= FeatureEntry->Value;
321 // For each feature that this implies, set it.
322 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
324 Bits &= ~FeatureEntry->Value;
326 // For each feature that implies this, clear it.
327 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
330 errs() << "'" << Feature
331 << "' is not a recognized feature for this target"
332 << " (ignoring feature)\n";
339 /// Get scheduling itinerary of a CPU.
340 const void *SubtargetFeatures::getItinerary(const StringRef CPU,
341 const SubtargetInfoKV *Table,
343 assert(Table && "missing table");
345 for (size_t i = 1; i < TableSize; i++) {
346 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
351 const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize);
357 << "' is not a recognized processor for this target"
358 << " (ignoring processor)\n";
363 /// print - Print feature string.
365 void SubtargetFeatures::print(raw_ostream &OS) const {
366 for (size_t i = 0, e = Features.size(); i != e; ++i)
367 OS << Features[i] << " ";
371 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
372 /// dump - Dump feature info.
374 void SubtargetFeatures::dump() const {
379 /// getDefaultSubtargetFeatures - Return a string listing the features
380 /// associated with the target triple.
382 /// FIXME: This is an inelegant way of specifying the features of a
383 /// subtarget. It would be better if we could encode this information
384 /// into the IR. See <rdar://5972456>.
386 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
387 if (Triple.getVendor() == Triple::Apple) {
388 if (Triple.getArch() == Triple::ppc) {
390 AddFeature("altivec");
391 } else if (Triple.getArch() == Triple::ppc64) {
394 AddFeature("altivec");