blob: 80621232c9e98b049ed491d85f3bb5d730927944 [file] [log] [blame]
Jim Laskeyb3302db2005-09-01 21:36:18 +00001//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Jim Laskey98a69792006-03-24 10:00:56 +00005// This file was developed by James M. Laskey and is distributed under the
Jim Laskeyb3302db2005-09-01 21:36:18 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SubtargetFeature interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/SubtargetFeature.h"
Chris Lattner54195c12005-10-23 05:26:26 +000015#include "llvm/ADT/StringExtras.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000016#include "llvm/Support/Streams.h"
Jim Laskeyb3302db2005-09-01 21:36:18 +000017#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000018#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000019#include <cctype>
Jim Laskeyb3302db2005-09-01 21:36:18 +000020using namespace llvm;
21
Chris Lattner54195c12005-10-23 05:26:26 +000022//===----------------------------------------------------------------------===//
23// Static Helper Functions
24//===----------------------------------------------------------------------===//
25
26/// hasFlag - Determine if a feature has a flag; '+' or '-'
27///
28static inline bool hasFlag(const std::string &Feature) {
29 assert(!Feature.empty() && "Empty string");
30 // Get first character
31 char Ch = Feature[0];
32 // Check if first character is '+' or '-' flag
33 return Ch == '+' || Ch =='-';
34}
35
36/// StripFlag - Return string stripped of flag.
37///
38static inline std::string StripFlag(const std::string &Feature) {
39 return hasFlag(Feature) ? Feature.substr(1) : Feature;
40}
41
42/// isEnabled - Return true if enable flag; '+'.
43///
44static inline bool isEnabled(const std::string &Feature) {
45 assert(!Feature.empty() && "Empty string");
46 // Get first character
47 char Ch = Feature[0];
48 // Check if first character is '+' for enabled
49 return Ch == '+';
50}
51
52/// PrependFlag - Return a string with a prepended flag; '+' or '-'.
53///
54static inline std::string PrependFlag(const std::string &Feature,
55 bool IsEnabled) {
56 assert(!Feature.empty() && "Empty string");
57 if (hasFlag(Feature)) return Feature;
58 return std::string(IsEnabled ? "+" : "-") + Feature;
59}
60
61/// Split - Splits a string of comma separated items in to a vector of strings.
62///
63static void Split(std::vector<std::string> &V, const std::string &S) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000064 // Start at beginning of string.
65 size_t Pos = 0;
66 while (true) {
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));
73 break;
74 }
75 // Otherwise add substring to vector
76 V.push_back(S.substr(Pos, Comma - Pos));
77 // Advance to next item
78 Pos = Comma + 1;
79 }
80}
81
82/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner54195c12005-10-23 05:26:26 +000083///
84static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000085 // Start with empty string.
86 std::string Result;
87 // If the vector is not empty
88 if (!V.empty()) {
89 // Start with the CPU feature
90 Result = V[0];
91 // For each successive feature
92 for (size_t i = 1; i < V.size(); i++) {
93 // Add a comma
94 Result += ",";
95 // Add the feature
96 Result += V[i];
97 }
98 }
99 // Return the features string
100 return Result;
101}
102
Jim Laskeyb3302db2005-09-01 21:36:18 +0000103/// Adding features.
104void SubtargetFeatures::AddFeature(const std::string &String,
105 bool IsEnabled) {
106 // Don't add empty features
107 if (!String.empty()) {
108 // Convert to lowercase, prepend flag and add to vector
Chris Lattner54195c12005-10-23 05:26:26 +0000109 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000110 }
111}
112
Jim Laskey34bd5d52005-10-25 15:15:28 +0000113/// Find KV in array using binary search.
114template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000115 // Make the lower bound element we're looking for
116 T KV;
117 KV.Key = S.c_str();
Jim Laskeyb3302db2005-09-01 21:36:18 +0000118 // Determine the end of the array
Jim Laskey34bd5d52005-10-25 15:15:28 +0000119 const T *Hi = A + L;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000120 // Binary search the array
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000121 const T *F = std::lower_bound(A, Hi, KV);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000122 // If not found then return NULL
123 if (F == Hi || std::string(F->Key) != S) return NULL;
124 // Return the found array item
125 return F;
126}
127
Chris Lattner3e808a42005-10-23 22:23:13 +0000128/// getLongestEntryLength - Return the length of the longest entry in the table.
129///
130static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
131 size_t Size) {
132 size_t MaxLen = 0;
133 for (size_t i = 0; i < Size; i++)
134 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
135 return MaxLen;
136}
137
Jim Laskey839615a2005-09-02 19:27:43 +0000138/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000139///
Chris Lattner3e808a42005-10-23 22:23:13 +0000140static 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);
Chris Lattnerba76c212005-10-23 05:33:39 +0000145
Chris Lattner3e808a42005-10-23 22:23:13 +0000146 // Print the CPU table.
Bill Wendlingf5da1332006-12-07 22:21:48 +0000147 cerr << "Available CPUs for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000148 for (size_t i = 0; i != CPUTableSize; i++)
Bill Wendlingf5da1332006-12-07 22:21:48 +0000149 cerr << " " << CPUTable[i].Key
150 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
151 << " - " << CPUTable[i].Desc << ".\n";
152 cerr << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000153
154 // Print the Feature table.
Bill Wendlingf5da1332006-12-07 22:21:48 +0000155 cerr << "Available features for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000156 for (size_t i = 0; i != FeatTableSize; i++)
Bill Wendlingf5da1332006-12-07 22:21:48 +0000157 cerr << " " << FeatTable[i].Key
158 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
159 << " - " << FeatTable[i].Desc << ".\n";
160 cerr << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000161
Bill Wendlingf5da1332006-12-07 22:21:48 +0000162 cerr << "Use +feature to enable a feature, or -feature to disable it.\n"
163 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Chris Lattner54195c12005-10-23 05:26:26 +0000164 exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000165}
166
Chris Lattner54195c12005-10-23 05:26:26 +0000167//===----------------------------------------------------------------------===//
168// SubtargetFeatures Implementation
169//===----------------------------------------------------------------------===//
170
171SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
172 // Break up string into separate features
173 Split(Features, Initial);
174}
175
176
177std::string SubtargetFeatures::getString() const {
178 return Join(Features);
179}
180void SubtargetFeatures::setString(const std::string &Initial) {
181 // Throw out old features
182 Features.clear();
183 // Break up string into separate features
184 Split(Features, LowercaseString(Initial));
185}
186
Jim Laskey34bd5d52005-10-25 15:15:28 +0000187
Chris Lattner54195c12005-10-23 05:26:26 +0000188/// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
189/// clears CPU.
190void SubtargetFeatures::setCPU(const std::string &String) {
191 Features[0] = LowercaseString(String);
192}
193
194
Jim Laskey34bd5d52005-10-25 15:15:28 +0000195/// setCPUIfNone - Setting CPU string only if no string is set.
Chris Lattner54195c12005-10-23 05:26:26 +0000196///
Jim Laskey34bd5d52005-10-25 15:15:28 +0000197void SubtargetFeatures::setCPUIfNone(const std::string &String) {
198 if (Features[0].empty()) setCPU(String);
199}
200
201
202/// getBits - Get feature bits.
203///
204uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
205 size_t CPUTableSize,
206 const SubtargetFeatureKV *FeatureTable,
207 size_t FeatureTableSize) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000208 assert(CPUTable && "missing CPU table");
209 assert(FeatureTable && "missing features table");
210#ifndef NDEBUG
211 for (size_t i = 1; i < CPUTableSize; i++) {
212 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
213 "CPU table is not sorted");
214 }
215 for (size_t i = 1; i < FeatureTableSize; i++) {
216 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
217 "CPU features table is not sorted");
218 }
219#endif
Jim Laskeyb3302db2005-09-01 21:36:18 +0000220 uint32_t Bits = 0; // Resulting bits
Chris Lattner3e808a42005-10-23 22:23:13 +0000221
Jim Laskey34bd5d52005-10-25 15:15:28 +0000222 // Check if help is needed
223 if (Features[0] == "help")
Chris Lattner3e808a42005-10-23 22:23:13 +0000224 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
225
Jim Laskeyb3302db2005-09-01 21:36:18 +0000226 // Find CPU entry
227 const SubtargetFeatureKV *CPUEntry =
228 Find(Features[0], CPUTable, CPUTableSize);
229 // If there is a match
230 if (CPUEntry) {
231 // Set base feature bits
232 Bits = CPUEntry->Value;
233 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000234 cerr << "'" << Features[0]
235 << "' is not a recognized processor for this target"
236 << " (ignoring processor)"
237 << "\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000238 }
239 // Iterate through each feature
240 for (size_t i = 1; i < Features.size(); i++) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000241 const std::string &Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000242
Jim Laskey839615a2005-09-02 19:27:43 +0000243 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000244 if (Feature == "+help")
245 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
246
Jim Laskeyb3302db2005-09-01 21:36:18 +0000247 // Find feature in table.
248 const SubtargetFeatureKV *FeatureEntry =
249 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
250 // If there is a match
251 if (FeatureEntry) {
252 // Enable/disable feature in bits
253 if (isEnabled(Feature)) Bits |= FeatureEntry->Value;
254 else Bits &= ~FeatureEntry->Value;
255 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000256 cerr << "'" << Feature
257 << "' is not a recognized feature for this target"
258 << " (ignoring feature)"
259 << "\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000260 }
261 }
262 return Bits;
263}
264
Jim Laskey34bd5d52005-10-25 15:15:28 +0000265/// Get info pointer
266void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table,
267 size_t TableSize) {
268 assert(Table && "missing table");
269#ifndef NDEBUG
270 for (size_t i = 1; i < TableSize; i++) {
271 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
272 }
273#endif
274
275 // Find entry
276 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
277
278 if (Entry) {
279 return Entry->Value;
280 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000281 cerr << "'" << Features[0]
282 << "' is not a recognized processor for this target"
283 << " (ignoring processor)"
284 << "\n";
Jim Laskey34bd5d52005-10-25 15:15:28 +0000285 return NULL;
286 }
287}
288
289/// print - Print feature string.
290///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000291void SubtargetFeatures::print(std::ostream &OS) const {
292 for (size_t i = 0; i < Features.size(); i++) {
293 OS << Features[i] << " ";
294 }
295 OS << "\n";
296}
297
Jim Laskey34bd5d52005-10-25 15:15:28 +0000298/// dump - Dump feature info.
299///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000300void SubtargetFeatures::dump() const {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000301 print(*cerr.stream());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000302}