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/ADT/SmallSet.h"
15 #include "llvm/MC/SubtargetFeature.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/raw_ostream.h"
26 //===----------------------------------------------------------------------===//
27 // Static Helper Functions
28 //===----------------------------------------------------------------------===//
30 /// hasFlag - Determine if a feature has a flag; '+' or '-'
32 static inline bool hasFlag(const StringRef Feature) {
33 assert(!Feature.empty() && "Empty string");
34 // Get first character
36 // Check if first character is '+' or '-' flag
37 return Ch == '+' || Ch =='-';
40 /// StripFlag - Return string stripped of flag.
42 static inline std::string StripFlag(const StringRef Feature) {
43 return hasFlag(Feature) ? Feature.substr(1) : Feature;
46 /// isEnabled - Return true if enable flag; '+'.
48 static inline bool isEnabled(const StringRef Feature) {
49 assert(!Feature.empty() && "Empty string");
50 // Get first character
52 // Check if first character is '+' for enabled
56 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
58 static inline std::string PrependFlag(const StringRef Feature,
60 assert(!Feature.empty() && "Empty string");
63 std::string Prefix = IsEnabled ? "+" : "-";
68 /// Split - Splits a string of comma separated items in to a vector of strings.
70 static void Split(std::vector<std::string> &V, const StringRef S) {
74 // Start at beginning of string.
77 // Find the next comma
78 size_t Comma = S.find(',', Pos);
79 // If no comma found then the rest of the string is used
80 if (Comma == std::string::npos) {
81 // Add string to vector
82 V.push_back(S.substr(Pos));
85 // Otherwise add substring to vector
86 V.push_back(S.substr(Pos, Comma - Pos));
87 // Advance to next item
92 /// Join a vector of strings to a string with a comma separating each element.
94 static std::string Join(const std::vector<std::string> &V) {
95 // Start with empty string.
97 // If the vector is not empty
99 // Start with the first feature
101 // For each successive feature
102 for (size_t i = 1; i < V.size(); i++) {
109 // Return the features string
114 void SubtargetFeatures::AddFeature(const StringRef String,
116 // Don't add empty features
117 if (!String.empty()) {
118 // Convert to lowercase, prepend flag and add to vector
119 Features.push_back(PrependFlag(String.lower(), IsEnabled));
123 // This needs to be shared between the instantiations of Find<>
124 typedef std::pair<std::string,std::string> KeyWithType;
125 static SmallSet<KeyWithType,10> reportedAsUnrecognized;
127 /// Find KV in array using binary search.
129 const T *SubtargetFeatures::Find(StringRef Key, const T *Array, size_t Length,
130 const char* KeyType) {
131 // Determine the end of the array
132 const T *Hi = Array + Length;
133 // Binary search the array
134 const T *F = std::lower_bound(Array, Hi, Key);
135 // If not found then return NULL
136 if (F == Hi || StringRef(F->Key) != Key) {
137 // If not yet reported, report now
138 KeyWithType current(KeyType, Key);
139 if(!reportedAsUnrecognized.count(current)) {
140 SmallString<1024> storage;
141 StringRef message = ("'" + Key +
142 "' is not a recognized " + KeyType +
143 " for this target (ignoring " + KeyType +
144 ")").toStringRef(storage);
145 SMDiagnostic(StringRef(), SourceMgr::DK_Warning, message)
147 reportedAsUnrecognized.insert(current);
151 // Return the found array item
155 // Instantiate with <SubtargetInfoKV> for use in MCSubtargetInfo
156 template const SubtargetInfoKV *SubtargetFeatures::Find<SubtargetInfoKV>
157 (StringRef Key, const SubtargetInfoKV *Array, size_t Length, const char* KeyType);
159 /// getLongestEntryLength - Return the length of the longest entry in the table.
161 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
164 for (size_t i = 0; i < Size; i++)
165 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
169 /// Display help for feature choices.
171 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
172 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
173 // Determine the length of the longest CPU and Feature entries.
174 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
175 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
177 // Print the CPU table.
178 errs() << "Available CPUs for this target:\n\n";
179 for (size_t i = 0; i != CPUTableSize; i++)
180 errs() << format(" %-*s - %s.\n",
181 MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc);
184 // Print the Feature table.
185 errs() << "Available features for this target:\n\n";
186 for (size_t i = 0; i != FeatTableSize; i++)
187 errs() << format(" %-*s - %s.\n",
188 MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc);
191 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
192 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
196 //===----------------------------------------------------------------------===//
197 // SubtargetFeatures Implementation
198 //===----------------------------------------------------------------------===//
200 SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
201 // Break up string into separate features
202 Split(Features, Initial);
206 std::string SubtargetFeatures::getString() const {
207 return Join(Features);
210 /// SetImpliedBits - For each feature that is (transitively) implied by this
214 void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
215 const SubtargetFeatureKV *FeatureTable,
216 size_t FeatureTableSize) {
217 for (size_t i = 0; i < FeatureTableSize; ++i) {
218 const SubtargetFeatureKV &FE = FeatureTable[i];
220 if (FeatureEntry->Value == FE.Value) continue;
222 if (FeatureEntry->Implies & FE.Value) {
224 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
229 /// ClearImpliedBits - For each feature that (transitively) implies this
230 /// feature, clear it.
233 void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
234 const SubtargetFeatureKV *FeatureTable,
235 size_t FeatureTableSize) {
236 for (size_t i = 0; i < FeatureTableSize; ++i) {
237 const SubtargetFeatureKV &FE = FeatureTable[i];
239 if (FeatureEntry->Value == FE.Value) continue;
241 if (FE.Implies & FeatureEntry->Value) {
243 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
248 /// ToggleFeature - Toggle a feature and returns the newly updated feature
251 SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature,
252 const SubtargetFeatureKV *FeatureTable,
253 size_t FeatureTableSize) {
254 // Find feature in table.
255 const SubtargetFeatureKV *FeatureEntry =
256 Find(StripFlag(Feature), FeatureTable, FeatureTableSize, "feature");
257 // If there is a match
259 if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
260 Bits &= ~FeatureEntry->Value;
262 // For each feature that implies this, clear it.
263 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
265 Bits |= FeatureEntry->Value;
267 // For each feature that this implies, set it.
268 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
276 /// getFeatureBits - Get feature bits a CPU.
278 uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
279 const SubtargetFeatureKV *CPUTable,
281 const SubtargetFeatureKV *FeatureTable,
282 size_t FeatureTableSize) {
283 if (!FeatureTableSize || !CPUTableSize)
287 for (size_t i = 1; i < CPUTableSize; i++) {
288 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
289 "CPU table is not sorted");
291 for (size_t i = 1; i < FeatureTableSize; i++) {
292 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
293 "CPU features table is not sorted");
296 uint64_t Bits = 0; // Resulting bits
298 // Check if help is needed
300 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
302 // Find CPU entry if CPU name is specified.
304 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize,
306 // If there is a match
308 // Set base feature bits
309 Bits = CPUEntry->Value;
311 // Set the feature implied by this CPU feature, if any.
312 for (size_t i = 0; i < FeatureTableSize; ++i) {
313 const SubtargetFeatureKV &FE = FeatureTable[i];
314 if (CPUEntry->Value & FE.Value)
315 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
320 // Iterate through each feature
321 for (size_t i = 0, E = Features.size(); i < E; i++) {
322 const StringRef Feature = Features[i];
325 if (Feature == "+help")
326 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
328 // Find feature in table.
329 const SubtargetFeatureKV *FeatureEntry =
330 Find(StripFlag(Feature), FeatureTable, FeatureTableSize, "feature");
331 // If there is a match
333 // Enable/disable feature in bits
334 if (isEnabled(Feature)) {
335 Bits |= FeatureEntry->Value;
337 // For each feature that this implies, set it.
338 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
340 Bits &= ~FeatureEntry->Value;
342 // For each feature that implies this, clear it.
343 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
351 /// print - Print feature string.
353 void SubtargetFeatures::print(raw_ostream &OS) const {
354 for (size_t i = 0, e = Features.size(); i != e; ++i)
355 OS << Features[i] << " ";
359 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
360 /// dump - Dump feature info.
362 void SubtargetFeatures::dump() const {
367 /// Adds the default features for the specified target triple.
369 /// FIXME: This is an inelegant way of specifying the features of a
370 /// subtarget. It would be better if we could encode this information
371 /// into the IR. See <rdar://5972456>.
373 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
374 if (Triple.getVendor() == Triple::Apple) {
375 if (Triple.getArch() == Triple::ppc) {
377 AddFeature("altivec");
378 } else if (Triple.getArch() == Triple::ppc64) {
381 AddFeature("altivec");