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/raw_ostream.h"
17 #include "llvm/ADT/StringExtras.h"
24 //===----------------------------------------------------------------------===//
25 // Static Helper Functions
26 //===----------------------------------------------------------------------===//
28 /// hasFlag - Determine if a feature has a flag; '+' or '-'
30 static inline bool hasFlag(const std::string &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 std::string &Feature) {
41 return hasFlag(Feature) ? Feature.substr(1) : Feature;
44 /// isEnabled - Return true if enable flag; '+'.
46 static inline bool isEnabled(const std::string &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 std::string &Feature,
58 assert(!Feature.empty() && "Empty string");
59 if (hasFlag(Feature)) return Feature;
60 return std::string(IsEnabled ? "+" : "-") + Feature;
63 /// Split - Splits a string of comma separated items in to a vector of strings.
65 static void Split(std::vector<std::string> &V, const std::string &S) {
69 // Start at beginning of string.
72 // Find the next comma
73 size_t Comma = S.find(',', Pos);
74 // If no comma found then the rest of the string is used
75 if (Comma == std::string::npos) {
76 // Add string to vector
77 V.push_back(S.substr(Pos));
80 // Otherwise add substring to vector
81 V.push_back(S.substr(Pos, Comma - Pos));
82 // Advance to next item
87 /// Join a vector of strings to a string with a comma separating each element.
89 static std::string Join(const std::vector<std::string> &V) {
90 // Start with empty string.
92 // If the vector is not empty
94 // Start with the first feature
96 // For each successive feature
97 for (size_t i = 1; i < V.size(); i++) {
104 // Return the features string
109 void SubtargetFeatures::AddFeature(const std::string &String,
111 // Don't add empty features
112 if (!String.empty()) {
113 // Convert to lowercase, prepend flag and add to vector
114 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
118 /// Find KV in array using binary search.
119 template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
120 // Make the lower bound element we're looking for
123 // Determine the end of the array
125 // Binary search the array
126 const T *F = std::lower_bound(A, Hi, KV);
127 // If not found then return NULL
128 if (F == Hi || std::string(F->Key) != S) return NULL;
129 // Return the found array item
133 /// getLongestEntryLength - Return the length of the longest entry in the table.
135 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
138 for (size_t i = 0; i < Size; i++)
139 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
143 /// Display help for feature choices.
145 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
146 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
147 // Determine the length of the longest CPU and Feature entries.
148 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
149 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
151 // Print the CPU table.
152 errs() << "Available CPUs for this target:\n\n";
153 for (size_t i = 0; i != CPUTableSize; i++)
154 errs() << " " << CPUTable[i].Key
155 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
156 << " - " << CPUTable[i].Desc << ".\n";
159 // Print the Feature table.
160 errs() << "Available features for this target:\n\n";
161 for (size_t i = 0; i != FeatTableSize; i++)
162 errs() << " " << FeatTable[i].Key
163 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
164 << " - " << FeatTable[i].Desc << ".\n";
167 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
168 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
172 //===----------------------------------------------------------------------===//
173 // SubtargetFeatures Implementation
174 //===----------------------------------------------------------------------===//
176 SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
177 // Break up string into separate features
178 Split(Features, Initial);
182 std::string SubtargetFeatures::getString() const {
183 return Join(Features);
185 void SubtargetFeatures::setString(const std::string &Initial) {
186 // Throw out old features
188 // Break up string into separate features
189 Split(Features, LowercaseString(Initial));
192 /// SetImpliedBits - For each feature that is (transitively) implied by this
196 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
197 const SubtargetFeatureKV *FeatureTable,
198 size_t FeatureTableSize) {
199 for (size_t i = 0; i < FeatureTableSize; ++i) {
200 const SubtargetFeatureKV &FE = FeatureTable[i];
202 if (FeatureEntry->Value == FE.Value) continue;
204 if (FeatureEntry->Implies & FE.Value) {
206 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
211 /// ClearImpliedBits - For each feature that (transitively) implies this
212 /// feature, clear it.
215 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
216 const SubtargetFeatureKV *FeatureTable,
217 size_t FeatureTableSize) {
218 for (size_t i = 0; i < FeatureTableSize; ++i) {
219 const SubtargetFeatureKV &FE = FeatureTable[i];
221 if (FeatureEntry->Value == FE.Value) continue;
223 if (FE.Implies & FeatureEntry->Value) {
225 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
230 /// getFeatureBits - Get feature bits a CPU.
232 uint64_t SubtargetFeatures::getFeatureBits(const std::string &CPU,
233 const SubtargetFeatureKV *CPUTable,
235 const SubtargetFeatureKV *FeatureTable,
236 size_t FeatureTableSize) {
237 assert(CPUTable && "missing CPU table");
238 assert(FeatureTable && "missing features table");
240 for (size_t i = 1; i < CPUTableSize; i++) {
241 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
242 "CPU table is not sorted");
244 for (size_t i = 1; i < FeatureTableSize; i++) {
245 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
246 "CPU features table is not sorted");
249 uint64_t Bits = 0; // Resulting bits
251 // Check if help is needed
253 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
256 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
257 // If there is a match
259 // Set base feature bits
260 Bits = CPUEntry->Value;
262 // Set the feature implied by this CPU feature, if any.
263 for (size_t i = 0; i < FeatureTableSize; ++i) {
264 const SubtargetFeatureKV &FE = FeatureTable[i];
265 if (CPUEntry->Value & FE.Value)
266 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
270 << "' is not a recognized processor for this target"
271 << " (ignoring processor)\n";
273 // Iterate through each feature
274 for (size_t i = 0, E = Features.size(); i < E; i++) {
275 const std::string &Feature = Features[i];
278 if (Feature == "+help")
279 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
281 // Find feature in table.
282 const SubtargetFeatureKV *FeatureEntry =
283 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
284 // If there is a match
286 // Enable/disable feature in bits
287 if (isEnabled(Feature)) {
288 Bits |= FeatureEntry->Value;
290 // For each feature that this implies, set it.
291 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
293 Bits &= ~FeatureEntry->Value;
295 // For each feature that implies this, clear it.
296 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
299 errs() << "'" << Feature
300 << "' is not a recognized feature for this target"
301 << " (ignoring feature)\n";
308 /// Get scheduling itinerary of a CPU.
309 void *SubtargetFeatures::getItinerary(const std::string &CPU,
310 const SubtargetInfoKV *Table,
312 assert(Table && "missing table");
314 for (size_t i = 1; i < TableSize; i++) {
315 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
320 const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize);
326 << "' is not a recognized processor for this target"
327 << " (ignoring processor)\n";
332 /// print - Print feature string.
334 void SubtargetFeatures::print(raw_ostream &OS) const {
335 for (size_t i = 0, e = Features.size(); i != e; ++i)
336 OS << Features[i] << " ";
340 /// dump - Dump feature info.
342 void SubtargetFeatures::dump() const {
346 /// getDefaultSubtargetFeatures - Return a string listing the features
347 /// associated with the target triple.
349 /// FIXME: This is an inelegant way of specifying the features of a
350 /// subtarget. It would be better if we could encode this information
351 /// into the IR. See <rdar://5972456>.
353 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
354 if (Triple.getVendor() == Triple::Apple) {
355 if (Triple.getArch() == Triple::ppc) {
357 AddFeature("altivec");
358 } else if (Triple.getArch() == Triple::ppc64) {
361 AddFeature("altivec");