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/ADT/StringExtras.h"
16 #include "llvm/Support/Streams.h"
23 //===----------------------------------------------------------------------===//
24 // Static Helper Functions
25 //===----------------------------------------------------------------------===//
27 /// hasFlag - Determine if a feature has a flag; '+' or '-'
29 static inline bool hasFlag(const std::string
&Feature
) {
30 assert(!Feature
.empty() && "Empty string");
31 // Get first character
33 // Check if first character is '+' or '-' flag
34 return Ch
== '+' || Ch
=='-';
37 /// StripFlag - Return string stripped of flag.
39 static inline std::string
StripFlag(const std::string
&Feature
) {
40 return hasFlag(Feature
) ? Feature
.substr(1) : Feature
;
43 /// isEnabled - Return true if enable flag; '+'.
45 static inline bool isEnabled(const std::string
&Feature
) {
46 assert(!Feature
.empty() && "Empty string");
47 // Get first character
49 // Check if first character is '+' for enabled
53 /// PrependFlag - Return a string with a prepended flag; '+' or '-'.
55 static inline std::string
PrependFlag(const std::string
&Feature
,
57 assert(!Feature
.empty() && "Empty string");
58 if (hasFlag(Feature
)) return Feature
;
59 return std::string(IsEnabled
? "+" : "-") + Feature
;
62 /// Split - Splits a string of comma separated items in to a vector of strings.
64 static void Split(std::vector
<std::string
> &V
, const std::string
&S
) {
65 // Start at beginning of string.
68 // Find the next comma
69 size_t Comma
= S
.find(',', Pos
);
70 // If no comma found then the the rest of the string is used
71 if (Comma
== std::string::npos
) {
72 // Add string to vector
73 V
.push_back(S
.substr(Pos
));
76 // Otherwise add substring to vector
77 V
.push_back(S
.substr(Pos
, Comma
- Pos
));
78 // Advance to next item
83 /// Join a vector of strings to a string with a comma separating each element.
85 static std::string
Join(const std::vector
<std::string
> &V
) {
86 // Start with empty string.
88 // If the vector is not empty
90 // Start with the CPU feature
92 // For each successive feature
93 for (size_t i
= 1; i
< V
.size(); i
++) {
100 // Return the features string
105 void SubtargetFeatures::AddFeature(const std::string
&String
,
107 // Don't add empty features
108 if (!String
.empty()) {
109 // Convert to lowercase, prepend flag and add to vector
110 Features
.push_back(PrependFlag(LowercaseString(String
), IsEnabled
));
114 /// Find KV in array using binary search.
115 template<typename T
> const T
*Find(const std::string
&S
, const T
*A
, size_t L
) {
116 // Make the lower bound element we're looking for
119 // Determine the end of the array
121 // Binary search the array
122 const T
*F
= std::lower_bound(A
, Hi
, KV
);
123 // If not found then return NULL
124 if (F
== Hi
|| std::string(F
->Key
) != S
) return NULL
;
125 // Return the found array item
129 /// getLongestEntryLength - Return the length of the longest entry in the table.
131 static size_t getLongestEntryLength(const SubtargetFeatureKV
*Table
,
134 for (size_t i
= 0; i
< Size
; i
++)
135 MaxLen
= std::max(MaxLen
, std::strlen(Table
[i
].Key
));
139 /// Display help for feature choices.
141 static void Help(const SubtargetFeatureKV
*CPUTable
, size_t CPUTableSize
,
142 const SubtargetFeatureKV
*FeatTable
, size_t FeatTableSize
) {
143 // Determine the length of the longest CPU and Feature entries.
144 unsigned MaxCPULen
= getLongestEntryLength(CPUTable
, CPUTableSize
);
145 unsigned MaxFeatLen
= getLongestEntryLength(FeatTable
, FeatTableSize
);
147 // Print the CPU table.
148 cerr
<< "Available CPUs for this target:\n\n";
149 for (size_t i
= 0; i
!= CPUTableSize
; i
++)
150 cerr
<< " " << CPUTable
[i
].Key
151 << std::string(MaxCPULen
- std::strlen(CPUTable
[i
].Key
), ' ')
152 << " - " << CPUTable
[i
].Desc
<< ".\n";
155 // Print the Feature table.
156 cerr
<< "Available features for this target:\n\n";
157 for (size_t i
= 0; i
!= FeatTableSize
; i
++)
158 cerr
<< " " << FeatTable
[i
].Key
159 << std::string(MaxFeatLen
- std::strlen(FeatTable
[i
].Key
), ' ')
160 << " - " << FeatTable
[i
].Desc
<< ".\n";
163 cerr
<< "Use +feature to enable a feature, or -feature to disable it.\n"
164 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
168 //===----------------------------------------------------------------------===//
169 // SubtargetFeatures Implementation
170 //===----------------------------------------------------------------------===//
172 SubtargetFeatures::SubtargetFeatures(const std::string
&Initial
) {
173 // Break up string into separate features
174 Split(Features
, Initial
);
178 std::string
SubtargetFeatures::getString() const {
179 return Join(Features
);
181 void SubtargetFeatures::setString(const std::string
&Initial
) {
182 // Throw out old features
184 // Break up string into separate features
185 Split(Features
, LowercaseString(Initial
));
189 /// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
191 void SubtargetFeatures::setCPU(const std::string
&String
) {
192 Features
[0] = LowercaseString(String
);
196 /// setCPUIfNone - Setting CPU string only if no string is set.
198 void SubtargetFeatures::setCPUIfNone(const std::string
&String
) {
199 if (Features
[0].empty()) setCPU(String
);
202 /// SetImpliedBits - For each feature that is (transitively) implied by this
206 void SetImpliedBits(uint32_t &Bits
, const SubtargetFeatureKV
*FeatureEntry
,
207 const SubtargetFeatureKV
*FeatureTable
,
208 size_t FeatureTableSize
) {
209 for (size_t i
= 0; i
< FeatureTableSize
; ++i
) {
210 const SubtargetFeatureKV
&FE
= FeatureTable
[i
];
212 if (FeatureEntry
->Value
== FE
.Value
) continue;
214 if (FeatureEntry
->Implies
& FE
.Value
) {
216 SetImpliedBits(Bits
, &FE
, FeatureTable
, FeatureTableSize
);
221 /// ClearImpliedBits - For each feature that (transitively) implies this
222 /// feature, clear it.
225 void ClearImpliedBits(uint32_t &Bits
, const SubtargetFeatureKV
*FeatureEntry
,
226 const SubtargetFeatureKV
*FeatureTable
,
227 size_t FeatureTableSize
) {
228 for (size_t i
= 0; i
< FeatureTableSize
; ++i
) {
229 const SubtargetFeatureKV
&FE
= FeatureTable
[i
];
231 if (FeatureEntry
->Value
== FE
.Value
) continue;
233 if (FE
.Implies
& FeatureEntry
->Value
) {
235 ClearImpliedBits(Bits
, &FE
, FeatureTable
, FeatureTableSize
);
240 /// getBits - Get feature bits.
242 uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV
*CPUTable
,
244 const SubtargetFeatureKV
*FeatureTable
,
245 size_t FeatureTableSize
) {
246 assert(CPUTable
&& "missing CPU table");
247 assert(FeatureTable
&& "missing features table");
249 for (size_t i
= 1; i
< CPUTableSize
; i
++) {
250 assert(strcmp(CPUTable
[i
- 1].Key
, CPUTable
[i
].Key
) < 0 &&
251 "CPU table is not sorted");
253 for (size_t i
= 1; i
< FeatureTableSize
; i
++) {
254 assert(strcmp(FeatureTable
[i
- 1].Key
, FeatureTable
[i
].Key
) < 0 &&
255 "CPU features table is not sorted");
258 uint32_t Bits
= 0; // Resulting bits
260 // Check if help is needed
261 if (Features
[0] == "help")
262 Help(CPUTable
, CPUTableSize
, FeatureTable
, FeatureTableSize
);
265 const SubtargetFeatureKV
*CPUEntry
=
266 Find(Features
[0], CPUTable
, CPUTableSize
);
267 // If there is a match
269 // Set base feature bits
270 Bits
= CPUEntry
->Value
;
272 // Set the feature implied by this CPU feature, if any.
273 for (size_t i
= 0; i
< FeatureTableSize
; ++i
) {
274 const SubtargetFeatureKV
&FE
= FeatureTable
[i
];
275 if (CPUEntry
->Value
& FE
.Value
)
276 SetImpliedBits(Bits
, &FE
, FeatureTable
, FeatureTableSize
);
279 cerr
<< "'" << Features
[0]
280 << "' is not a recognized processor for this target"
281 << " (ignoring processor)"
284 // Iterate through each feature
285 for (size_t i
= 1; i
< Features
.size(); i
++) {
286 const std::string
&Feature
= Features
[i
];
289 if (Feature
== "+help")
290 Help(CPUTable
, CPUTableSize
, FeatureTable
, FeatureTableSize
);
292 // Find feature in table.
293 const SubtargetFeatureKV
*FeatureEntry
=
294 Find(StripFlag(Feature
), FeatureTable
, FeatureTableSize
);
295 // If there is a match
297 // Enable/disable feature in bits
298 if (isEnabled(Feature
)) {
299 Bits
|= FeatureEntry
->Value
;
301 // For each feature that this implies, set it.
302 SetImpliedBits(Bits
, FeatureEntry
, FeatureTable
, FeatureTableSize
);
304 Bits
&= ~FeatureEntry
->Value
;
306 // For each feature that implies this, clear it.
307 ClearImpliedBits(Bits
, FeatureEntry
, FeatureTable
, FeatureTableSize
);
310 cerr
<< "'" << Feature
311 << "' is not a recognized feature for this target"
312 << " (ignoring feature)"
321 void *SubtargetFeatures::getInfo(const SubtargetInfoKV
*Table
,
323 assert(Table
&& "missing table");
325 for (size_t i
= 1; i
< TableSize
; i
++) {
326 assert(strcmp(Table
[i
- 1].Key
, Table
[i
].Key
) < 0 && "Table is not sorted");
331 const SubtargetInfoKV
*Entry
= Find(Features
[0], Table
, TableSize
);
336 cerr
<< "'" << Features
[0]
337 << "' is not a recognized processor for this target"
338 << " (ignoring processor)"
344 /// print - Print feature string.
346 void SubtargetFeatures::print(std::ostream
&OS
) const {
347 for (size_t i
= 0; i
< Features
.size(); i
++) {
348 OS
<< Features
[i
] << " ";
353 /// dump - Dump feature info.
355 void SubtargetFeatures::dump() const {
356 print(*cerr
.stream());