blob: 4669e0fdc3cfa45952c6913eb04c15ddd0b1bff5 [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>
Bill Wendling1a097e32006-12-07 23:41:45 +000018#include <ostream>
Jim Laskeyb3302db2005-09-01 21:36:18 +000019#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000020#include <cctype>
Jim Laskeyb3302db2005-09-01 21:36:18 +000021using namespace llvm;
22
Chris Lattner54195c12005-10-23 05:26:26 +000023//===----------------------------------------------------------------------===//
24// Static Helper Functions
25//===----------------------------------------------------------------------===//
26
27/// hasFlag - Determine if a feature has a flag; '+' or '-'
28///
29static inline bool hasFlag(const std::string &Feature) {
30 assert(!Feature.empty() && "Empty string");
31 // Get first character
32 char Ch = Feature[0];
33 // Check if first character is '+' or '-' flag
34 return Ch == '+' || Ch =='-';
35}
36
37/// StripFlag - Return string stripped of flag.
38///
39static inline std::string StripFlag(const std::string &Feature) {
40 return hasFlag(Feature) ? Feature.substr(1) : Feature;
41}
42
43/// isEnabled - Return true if enable flag; '+'.
44///
45static inline bool isEnabled(const std::string &Feature) {
46 assert(!Feature.empty() && "Empty string");
47 // Get first character
48 char Ch = Feature[0];
49 // Check if first character is '+' for enabled
50 return Ch == '+';
51}
52
53/// PrependFlag - Return a string with a prepended flag; '+' or '-'.
54///
55static inline std::string PrependFlag(const std::string &Feature,
56 bool IsEnabled) {
57 assert(!Feature.empty() && "Empty string");
58 if (hasFlag(Feature)) return Feature;
59 return std::string(IsEnabled ? "+" : "-") + Feature;
60}
61
62/// Split - Splits a string of comma separated items in to a vector of strings.
63///
64static void Split(std::vector<std::string> &V, const std::string &S) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000065 // Start at beginning of string.
66 size_t Pos = 0;
67 while (true) {
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));
74 break;
75 }
76 // Otherwise add substring to vector
77 V.push_back(S.substr(Pos, Comma - Pos));
78 // Advance to next item
79 Pos = Comma + 1;
80 }
81}
82
83/// Join a vector of strings to a string with a comma separating each element.
Chris Lattner54195c12005-10-23 05:26:26 +000084///
85static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000086 // Start with empty string.
87 std::string Result;
88 // If the vector is not empty
89 if (!V.empty()) {
90 // Start with the CPU feature
91 Result = V[0];
92 // For each successive feature
93 for (size_t i = 1; i < V.size(); i++) {
94 // Add a comma
95 Result += ",";
96 // Add the feature
97 Result += V[i];
98 }
99 }
100 // Return the features string
101 return Result;
102}
103
Jim Laskeyb3302db2005-09-01 21:36:18 +0000104/// Adding features.
105void SubtargetFeatures::AddFeature(const std::string &String,
106 bool IsEnabled) {
107 // Don't add empty features
108 if (!String.empty()) {
109 // Convert to lowercase, prepend flag and add to vector
Chris Lattner54195c12005-10-23 05:26:26 +0000110 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000111 }
112}
113
Jim Laskey34bd5d52005-10-25 15:15:28 +0000114/// Find KV in array using binary search.
115template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000116 // Make the lower bound element we're looking for
117 T KV;
118 KV.Key = S.c_str();
Jim Laskeyb3302db2005-09-01 21:36:18 +0000119 // Determine the end of the array
Jim Laskey34bd5d52005-10-25 15:15:28 +0000120 const T *Hi = A + L;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000121 // Binary search the array
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000122 const T *F = std::lower_bound(A, Hi, KV);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000123 // If not found then return NULL
124 if (F == Hi || std::string(F->Key) != S) return NULL;
125 // Return the found array item
126 return F;
127}
128
Chris Lattner3e808a42005-10-23 22:23:13 +0000129/// getLongestEntryLength - Return the length of the longest entry in the table.
130///
131static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
132 size_t Size) {
133 size_t MaxLen = 0;
134 for (size_t i = 0; i < Size; i++)
135 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
136 return MaxLen;
137}
138
Jim Laskey839615a2005-09-02 19:27:43 +0000139/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000140///
Chris Lattner3e808a42005-10-23 22:23:13 +0000141static 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);
Chris Lattnerba76c212005-10-23 05:33:39 +0000146
Chris Lattner3e808a42005-10-23 22:23:13 +0000147 // Print the CPU table.
Bill Wendlingf5da1332006-12-07 22:21:48 +0000148 cerr << "Available CPUs for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000149 for (size_t i = 0; i != CPUTableSize; i++)
Bill Wendlingf5da1332006-12-07 22:21:48 +0000150 cerr << " " << CPUTable[i].Key
151 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
152 << " - " << CPUTable[i].Desc << ".\n";
153 cerr << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000154
155 // Print the Feature table.
Bill Wendlingf5da1332006-12-07 22:21:48 +0000156 cerr << "Available features for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000157 for (size_t i = 0; i != FeatTableSize; i++)
Bill Wendlingf5da1332006-12-07 22:21:48 +0000158 cerr << " " << FeatTable[i].Key
159 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
160 << " - " << FeatTable[i].Desc << ".\n";
161 cerr << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000162
Bill Wendlingf5da1332006-12-07 22:21:48 +0000163 cerr << "Use +feature to enable a feature, or -feature to disable it.\n"
164 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Chris Lattner54195c12005-10-23 05:26:26 +0000165 exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000166}
167
Chris Lattner54195c12005-10-23 05:26:26 +0000168//===----------------------------------------------------------------------===//
169// SubtargetFeatures Implementation
170//===----------------------------------------------------------------------===//
171
172SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
173 // Break up string into separate features
174 Split(Features, Initial);
175}
176
177
178std::string SubtargetFeatures::getString() const {
179 return Join(Features);
180}
181void SubtargetFeatures::setString(const std::string &Initial) {
182 // Throw out old features
183 Features.clear();
184 // Break up string into separate features
185 Split(Features, LowercaseString(Initial));
186}
187
Jim Laskey34bd5d52005-10-25 15:15:28 +0000188
Chris Lattner54195c12005-10-23 05:26:26 +0000189/// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
190/// clears CPU.
191void SubtargetFeatures::setCPU(const std::string &String) {
192 Features[0] = LowercaseString(String);
193}
194
195
Jim Laskey34bd5d52005-10-25 15:15:28 +0000196/// setCPUIfNone - Setting CPU string only if no string is set.
Chris Lattner54195c12005-10-23 05:26:26 +0000197///
Jim Laskey34bd5d52005-10-25 15:15:28 +0000198void SubtargetFeatures::setCPUIfNone(const std::string &String) {
199 if (Features[0].empty()) setCPU(String);
200}
201
202
203/// getBits - Get feature bits.
204///
205uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
206 size_t CPUTableSize,
207 const SubtargetFeatureKV *FeatureTable,
208 size_t FeatureTableSize) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000209 assert(CPUTable && "missing CPU table");
210 assert(FeatureTable && "missing features table");
211#ifndef NDEBUG
212 for (size_t i = 1; i < CPUTableSize; i++) {
213 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
214 "CPU table is not sorted");
215 }
216 for (size_t i = 1; i < FeatureTableSize; i++) {
217 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
218 "CPU features table is not sorted");
219 }
220#endif
Jim Laskeyb3302db2005-09-01 21:36:18 +0000221 uint32_t Bits = 0; // Resulting bits
Chris Lattner3e808a42005-10-23 22:23:13 +0000222
Jim Laskey34bd5d52005-10-25 15:15:28 +0000223 // Check if help is needed
224 if (Features[0] == "help")
Chris Lattner3e808a42005-10-23 22:23:13 +0000225 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
226
Jim Laskeyb3302db2005-09-01 21:36:18 +0000227 // Find CPU entry
228 const SubtargetFeatureKV *CPUEntry =
229 Find(Features[0], CPUTable, CPUTableSize);
230 // If there is a match
231 if (CPUEntry) {
232 // Set base feature bits
233 Bits = CPUEntry->Value;
234 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000235 cerr << "'" << Features[0]
236 << "' is not a recognized processor for this target"
237 << " (ignoring processor)"
238 << "\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000239 }
240 // Iterate through each feature
241 for (size_t i = 1; i < Features.size(); i++) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000242 const std::string &Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000243
Jim Laskey839615a2005-09-02 19:27:43 +0000244 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000245 if (Feature == "+help")
246 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
247
Jim Laskeyb3302db2005-09-01 21:36:18 +0000248 // Find feature in table.
249 const SubtargetFeatureKV *FeatureEntry =
250 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
251 // If there is a match
252 if (FeatureEntry) {
253 // Enable/disable feature in bits
254 if (isEnabled(Feature)) Bits |= FeatureEntry->Value;
255 else Bits &= ~FeatureEntry->Value;
256 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000257 cerr << "'" << Feature
258 << "' is not a recognized feature for this target"
259 << " (ignoring feature)"
260 << "\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000261 }
262 }
263 return Bits;
264}
265
Jim Laskey34bd5d52005-10-25 15:15:28 +0000266/// Get info pointer
267void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table,
268 size_t TableSize) {
269 assert(Table && "missing table");
270#ifndef NDEBUG
271 for (size_t i = 1; i < TableSize; i++) {
272 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
273 }
274#endif
275
276 // Find entry
277 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
278
279 if (Entry) {
280 return Entry->Value;
281 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000282 cerr << "'" << Features[0]
283 << "' is not a recognized processor for this target"
284 << " (ignoring processor)"
285 << "\n";
Jim Laskey34bd5d52005-10-25 15:15:28 +0000286 return NULL;
287 }
288}
289
290/// print - Print feature string.
291///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000292void SubtargetFeatures::print(std::ostream &OS) const {
293 for (size_t i = 0; i < Features.size(); i++) {
294 OS << Features[i] << " ";
295 }
296 OS << "\n";
297}
298
Jim Laskey34bd5d52005-10-25 15:15:28 +0000299/// dump - Dump feature info.
300///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000301void SubtargetFeatures::dump() const {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000302 print(*cerr.stream());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000303}