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/Target/SubtargetFeature.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/ADT/StringExtras.h"
22 //===----------------------------------------------------------------------===//
23 // Static Helper Functions
24 //===----------------------------------------------------------------------===//
26 /// hasFlag - Determine if a feature has a flag; '+' or '-'
28 static inline bool hasFlag(const std::string
&Feature
) {
29 assert(!Feature
.empty() && "Empty string");
30 // Get first character
32 // Check if first character is '+' or '-' flag
33 return Ch
== '+' || Ch
=='-';
36 /// StripFlag - Return string stripped of flag.
38 static inline std::string
StripFlag(const std::string
&Feature
) {
39 return hasFlag(Feature
) ? Feature
.substr(1) : Feature
;
42 /// isEnabled - Return true if enable flag; '+'.
44 static inline bool isEnabled(const std::string
&Feature
) {
45 assert(!Feature
.empty() && "Empty string");
46 // Get first character
48 // Check if first character is '+' for enabled
52 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
54 static inline std::string
PrependFlag(const std::string
&Feature
,
56 assert(!Feature
.empty() && "Empty string");
57 if (hasFlag(Feature
)) return Feature
;
58 return std::string(IsEnabled
? "+" : "-") + Feature
;
61 /// Split - Splits a string of comma separated items in to a vector of strings.
63 static void Split(std::vector
<std::string
> &V
, const std::string
&S
) {
64 // Start at beginning of string.
67 // Find the next comma
68 size_t Comma
= S
.find(',', Pos
);
69 // If no comma found then the the rest of the string is used
70 if (Comma
== std::string::npos
) {
71 // Add string to vector
72 V
.push_back(S
.substr(Pos
));
75 // Otherwise add substring to vector
76 V
.push_back(S
.substr(Pos
, Comma
- Pos
));
77 // Advance to next item
82 /// Join a vector of strings to a string with a comma separating each element.
84 static std::string
Join(const std::vector
<std::string
> &V
) {
85 // Start with empty string.
87 // If the vector is not empty
89 // Start with the CPU feature
91 // For each successive feature
92 for (size_t i
= 1; i
< V
.size(); i
++) {
99 // Return the features string
104 void SubtargetFeatures::AddFeature(const std::string
&String
,
106 // Don't add empty features
107 if (!String
.empty()) {
108 // Convert to lowercase, prepend flag and add to vector
109 Features
.push_back(PrependFlag(LowercaseString(String
), IsEnabled
));
113 /// Find KV in array using binary search.
114 template<typename T
> const T
*Find(const std::string
&S
, const T
*A
, size_t L
) {
115 // Make the lower bound element we're looking for
118 // Determine the end of the array
120 // Binary search the array
121 const T
*F
= std::lower_bound(A
, Hi
, KV
);
122 // If not found then return NULL
123 if (F
== Hi
|| std::string(F
->Key
) != S
) return NULL
;
124 // Return the found array item
128 /// getLongestEntryLength - Return the length of the longest entry in the table.
130 static size_t getLongestEntryLength(const SubtargetFeatureKV
*Table
,
133 for (size_t i
= 0; i
< Size
; i
++)
134 MaxLen
= std::max(MaxLen
, std::strlen(Table
[i
].Key
));
138 /// Display help for feature choices.
140 static void Help(const SubtargetFeatureKV
*CPUTable
, size_t CPUTableSize
,
141 const SubtargetFeatureKV
*FeatTable
, size_t FeatTableSize
) {
142 // Determine the length of the longest CPU and Feature entries.
143 unsigned MaxCPULen
= getLongestEntryLength(CPUTable
, CPUTableSize
);
144 unsigned MaxFeatLen
= getLongestEntryLength(FeatTable
, FeatTableSize
);
146 // Print the CPU table.
147 errs() << "Available CPUs for this target:\n\n";
148 for (size_t i
= 0; i
!= CPUTableSize
; i
++)
149 errs() << " " << CPUTable
[i
].Key
150 << std::string(MaxCPULen
- std::strlen(CPUTable
[i
].Key
), ' ')
151 << " - " << CPUTable
[i
].Desc
<< ".\n";
154 // Print the Feature table.
155 errs() << "Available features for this target:\n\n";
156 for (size_t i
= 0; i
!= FeatTableSize
; i
++)
157 errs() << " " << FeatTable
[i
].Key
158 << std::string(MaxFeatLen
- std::strlen(FeatTable
[i
].Key
), ' ')
159 << " - " << FeatTable
[i
].Desc
<< ".\n";
162 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
163 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
167 //===----------------------------------------------------------------------===//
168 // SubtargetFeatures Implementation
169 //===----------------------------------------------------------------------===//
171 SubtargetFeatures::SubtargetFeatures(const std::string
&Initial
) {
172 // Break up string into separate features
173 Split(Features
, Initial
);
177 std::string
SubtargetFeatures::getString() const {
178 return Join(Features
);
180 void SubtargetFeatures::setString(const std::string
&Initial
) {
181 // Throw out old features
183 // Break up string into separate features
184 Split(Features
, LowercaseString(Initial
));
188 /// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
190 void SubtargetFeatures::setCPU(const std::string
&String
) {
191 Features
[0] = LowercaseString(String
);
195 /// setCPUIfNone - Setting CPU string only if no string is set.
197 void SubtargetFeatures::setCPUIfNone(const std::string
&String
) {
198 if (Features
[0].empty()) setCPU(String
);
201 /// getCPU - Returns current CPU.
203 const std::string
& SubtargetFeatures::getCPU() const {
208 /// SetImpliedBits - For each feature that is (transitively) implied by this
212 void SetImpliedBits(uint32_t &Bits
, const SubtargetFeatureKV
*FeatureEntry
,
213 const SubtargetFeatureKV
*FeatureTable
,
214 size_t FeatureTableSize
) {
215 for (size_t i
= 0; i
< FeatureTableSize
; ++i
) {
216 const SubtargetFeatureKV
&FE
= FeatureTable
[i
];
218 if (FeatureEntry
->Value
== FE
.Value
) continue;
220 if (FeatureEntry
->Implies
& FE
.Value
) {
222 SetImpliedBits(Bits
, &FE
, FeatureTable
, FeatureTableSize
);
227 /// ClearImpliedBits - For each feature that (transitively) implies this
228 /// feature, clear it.
231 void ClearImpliedBits(uint32_t &Bits
, const SubtargetFeatureKV
*FeatureEntry
,
232 const SubtargetFeatureKV
*FeatureTable
,
233 size_t FeatureTableSize
) {
234 for (size_t i
= 0; i
< FeatureTableSize
; ++i
) {
235 const SubtargetFeatureKV
&FE
= FeatureTable
[i
];
237 if (FeatureEntry
->Value
== FE
.Value
) continue;
239 if (FE
.Implies
& FeatureEntry
->Value
) {
241 ClearImpliedBits(Bits
, &FE
, FeatureTable
, FeatureTableSize
);
246 /// getBits - Get feature bits.
248 uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV
*CPUTable
,
250 const SubtargetFeatureKV
*FeatureTable
,
251 size_t FeatureTableSize
) {
252 assert(CPUTable
&& "missing CPU table");
253 assert(FeatureTable
&& "missing features table");
255 for (size_t i
= 1; i
< CPUTableSize
; i
++) {
256 assert(strcmp(CPUTable
[i
- 1].Key
, CPUTable
[i
].Key
) < 0 &&
257 "CPU table is not sorted");
259 for (size_t i
= 1; i
< FeatureTableSize
; i
++) {
260 assert(strcmp(FeatureTable
[i
- 1].Key
, FeatureTable
[i
].Key
) < 0 &&
261 "CPU features table is not sorted");
264 uint32_t Bits
= 0; // Resulting bits
266 // Check if help is needed
267 if (Features
[0] == "help")
268 Help(CPUTable
, CPUTableSize
, FeatureTable
, FeatureTableSize
);
271 const SubtargetFeatureKV
*CPUEntry
=
272 Find(Features
[0], CPUTable
, CPUTableSize
);
273 // If there is a match
275 // Set base feature bits
276 Bits
= CPUEntry
->Value
;
278 // Set the feature implied by this CPU feature, if any.
279 for (size_t i
= 0; i
< FeatureTableSize
; ++i
) {
280 const SubtargetFeatureKV
&FE
= FeatureTable
[i
];
281 if (CPUEntry
->Value
& FE
.Value
)
282 SetImpliedBits(Bits
, &FE
, FeatureTable
, FeatureTableSize
);
285 errs() << "'" << Features
[0]
286 << "' is not a recognized processor for this target"
287 << " (ignoring processor)\n";
289 // Iterate through each feature
290 for (size_t i
= 1; i
< Features
.size(); i
++) {
291 const std::string
&Feature
= Features
[i
];
294 if (Feature
== "+help")
295 Help(CPUTable
, CPUTableSize
, FeatureTable
, FeatureTableSize
);
297 // Find feature in table.
298 const SubtargetFeatureKV
*FeatureEntry
=
299 Find(StripFlag(Feature
), FeatureTable
, FeatureTableSize
);
300 // If there is a match
302 // Enable/disable feature in bits
303 if (isEnabled(Feature
)) {
304 Bits
|= FeatureEntry
->Value
;
306 // For each feature that this implies, set it.
307 SetImpliedBits(Bits
, FeatureEntry
, FeatureTable
, FeatureTableSize
);
309 Bits
&= ~FeatureEntry
->Value
;
311 // For each feature that implies this, clear it.
312 ClearImpliedBits(Bits
, FeatureEntry
, FeatureTable
, FeatureTableSize
);
315 errs() << "'" << Feature
316 << "' is not a recognized feature for this target"
317 << " (ignoring feature)\n";
325 void *SubtargetFeatures::getInfo(const SubtargetInfoKV
*Table
,
327 assert(Table
&& "missing table");
329 for (size_t i
= 1; i
< TableSize
; i
++) {
330 assert(strcmp(Table
[i
- 1].Key
, Table
[i
].Key
) < 0 && "Table is not sorted");
335 const SubtargetInfoKV
*Entry
= Find(Features
[0], Table
, TableSize
);
340 errs() << "'" << Features
[0]
341 << "' is not a recognized processor for this target"
342 << " (ignoring processor)\n";
347 /// print - Print feature string.
349 void SubtargetFeatures::print(raw_ostream
&OS
) const {
350 for (size_t i
= 0, e
= Features
.size(); i
!= e
; ++i
)
351 OS
<< Features
[i
] << " ";
355 /// dump - Dump feature info.
357 void SubtargetFeatures::dump() const {