blob: 93d19250f3da4502f8e7bf971cf4f90ee26b2618 [file] [log] [blame]
Jim Laskeyb3302db2005-09-01 21:36:18 +00001//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Jim Laskey and is distributed under the
6// 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"
Jim Laskeyb3302db2005-09-01 21:36:18 +000016#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000017#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000018#include <cctype>
Chris Lattner54195c12005-10-23 05:26:26 +000019#include <iostream>
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
113/// Find item in array using binary search.
Chris Lattner54195c12005-10-23 05:26:26 +0000114static const SubtargetFeatureKV *Find(const std::string &S,
115 const SubtargetFeatureKV *A, size_t L) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000116 // Determine the end of the array
117 const SubtargetFeatureKV *Hi = A + L;
118 // Binary search the array
119 const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S);
120 // If not found then return NULL
121 if (F == Hi || std::string(F->Key) != S) return NULL;
122 // Return the found array item
123 return F;
124}
125
Chris Lattner3e808a42005-10-23 22:23:13 +0000126/// getLongestEntryLength - Return the length of the longest entry in the table.
127///
128static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
129 size_t Size) {
130 size_t MaxLen = 0;
131 for (size_t i = 0; i < Size; i++)
132 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
133 return MaxLen;
134}
135
Jim Laskey839615a2005-09-02 19:27:43 +0000136/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000137///
Chris Lattner3e808a42005-10-23 22:23:13 +0000138static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
139 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
140 // Determine the length of the longest CPU and Feature entries.
141 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize);
142 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
Chris Lattnerba76c212005-10-23 05:33:39 +0000143
Chris Lattner3e808a42005-10-23 22:23:13 +0000144 // Print the CPU table.
145 std::cerr << "Available CPUs for this target:\n\n";
146 for (size_t i = 0; i != CPUTableSize; i++)
147 std::cerr << " " << CPUTable[i].Key
148 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
149 << " - " << CPUTable[i].Desc << ".\n";
Chris Lattnerba76c212005-10-23 05:33:39 +0000150 std::cerr << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000151
152 // Print the Feature table.
153 std::cerr << "Available features for this target:\n\n";
154 for (size_t i = 0; i != FeatTableSize; i++)
155 std::cerr << " " << FeatTable[i].Key
156 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
157 << " - " << FeatTable[i].Desc << ".\n";
158 std::cerr << "\n";
159
160 std::cerr << "Use +feature to enable a feature, or -feature to disable it.\n"
161 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Chris Lattner54195c12005-10-23 05:26:26 +0000162 exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000163}
164
Chris Lattner54195c12005-10-23 05:26:26 +0000165//===----------------------------------------------------------------------===//
166// SubtargetFeatures Implementation
167//===----------------------------------------------------------------------===//
168
169SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
170 // Break up string into separate features
171 Split(Features, Initial);
172}
173
174
175std::string SubtargetFeatures::getString() const {
176 return Join(Features);
177}
178void SubtargetFeatures::setString(const std::string &Initial) {
179 // Throw out old features
180 Features.clear();
181 // Break up string into separate features
182 Split(Features, LowercaseString(Initial));
183}
184
185/// setCPU - Set the CPU string. Replaces previous setting. Setting to ""
186/// clears CPU.
187void SubtargetFeatures::setCPU(const std::string &String) {
188 Features[0] = LowercaseString(String);
189}
190
191
192
Jim Laskeyb3302db2005-09-01 21:36:18 +0000193/// Parse feature string for quick usage.
Chris Lattner54195c12005-10-23 05:26:26 +0000194///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000195uint32_t SubtargetFeatures::Parse(const std::string &String,
196 const std::string &DefaultCPU,
197 const SubtargetFeatureKV *CPUTable,
198 size_t CPUTableSize,
199 const SubtargetFeatureKV *FeatureTable,
200 size_t FeatureTableSize) {
201 assert(CPUTable && "missing CPU table");
202 assert(FeatureTable && "missing features table");
203#ifndef NDEBUG
204 for (size_t i = 1; i < CPUTableSize; i++) {
205 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
206 "CPU table is not sorted");
207 }
208 for (size_t i = 1; i < FeatureTableSize; i++) {
209 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
210 "CPU features table is not sorted");
211 }
212#endif
213 std::vector<std::string> Features; // Subtarget features as a vector
214 uint32_t Bits = 0; // Resulting bits
215 // Split up features
216 Split(Features, String);
Chris Lattner3e808a42005-10-23 22:23:13 +0000217
Jim Laskeyb3302db2005-09-01 21:36:18 +0000218 // Check if default is needed
Chris Lattner3e808a42005-10-23 22:23:13 +0000219 if (Features[0].empty())
220 Features[0] = DefaultCPU;
221 else if (Features[0] == "help")
222 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
223
Jim Laskeyb3302db2005-09-01 21:36:18 +0000224 // Find CPU entry
225 const SubtargetFeatureKV *CPUEntry =
226 Find(Features[0], CPUTable, CPUTableSize);
227 // If there is a match
228 if (CPUEntry) {
229 // Set base feature bits
230 Bits = CPUEntry->Value;
231 } else {
Chris Lattner3eb33692005-09-07 05:44:14 +0000232 std::cerr << "'" << Features[0]
233 << "' is not a recognized processor for this target"
Jim Laskeyb3302db2005-09-01 21:36:18 +0000234 << " (ignoring processor)"
235 << "\n";
236 }
237 // Iterate through each feature
238 for (size_t i = 1; i < Features.size(); i++) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000239 const std::string &Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000240
Jim Laskey839615a2005-09-02 19:27:43 +0000241 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000242 if (Feature == "+help")
243 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
244
Jim Laskeyb3302db2005-09-01 21:36:18 +0000245 // Find feature in table.
246 const SubtargetFeatureKV *FeatureEntry =
247 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
248 // If there is a match
249 if (FeatureEntry) {
250 // Enable/disable feature in bits
251 if (isEnabled(Feature)) Bits |= FeatureEntry->Value;
252 else Bits &= ~FeatureEntry->Value;
253 } else {
Chris Lattner3eb33692005-09-07 05:44:14 +0000254 std::cerr << "'" << Feature
255 << "' is not a recognized feature for this target"
Jim Laskeyb3302db2005-09-01 21:36:18 +0000256 << " (ignoring feature)"
257 << "\n";
258 }
259 }
260 return Bits;
261}
262
263/// Print feature string.
264void SubtargetFeatures::print(std::ostream &OS) const {
265 for (size_t i = 0; i < Features.size(); i++) {
266 OS << Features[i] << " ";
267 }
268 OS << "\n";
269}
270
271/// Dump feature info.
272void SubtargetFeatures::dump() const {
273 print(std::cerr);
274}