54a89bdd0d52ad088f359d5b2e5250d94ec090d6
[oota-llvm.git] / lib / Target / SubtargetFeature.cpp
1 //===- 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"
15 #include "llvm/ADT/StringExtras.h"
16 #include <algorithm>
17 #include <cassert>
18 #include <cctype>
19 #include <iostream>
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 //                          Static Helper Functions
24 //===----------------------------------------------------------------------===//
25
26 /// hasFlag - Determine if a feature has a flag; '+' or '-'
27 ///
28 static 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 ///
38 static 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 ///
44 static 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 ///
54 static 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 ///
63 static void Split(std::vector<std::string> &V, const std::string &S) {
64   // 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.
83 ///
84 static std::string Join(const std::vector<std::string> &V) {
85   // 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
103 /// Adding features.
104 void 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
109     Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
110   }
111 }
112
113 /// Find item in array using binary search.
114 static const SubtargetFeatureKV *Find(const std::string &S,
115                                       const SubtargetFeatureKV *A, size_t L) {
116   // 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
126 /// Display help for feature choices.
127 ///
128 static void Help(bool isFeature, const SubtargetFeatureKV *Table,
129                  size_t TableSize) {
130   // Determine the length of the longest key.
131   size_t MaxLen = 0;
132   for (size_t i = 0; i < TableSize; i++)
133     MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
134   
135   std::cerr << "Available " << (isFeature ? "features" : "CPUs")
136             << " for this target:\n\n";
137
138   for (size_t i = 0; i < TableSize; i++) {
139     // Compute required padding
140     size_t Pad = MaxLen - std::strlen(Table[i].Key);
141     std::cerr << Table[i].Key << std::string(Pad, ' ') << " - "
142               << Table[i].Desc << ".\n";
143   }
144
145   std::cerr << "\n";
146   if (isFeature) {
147     std::cerr
148       << "Use +feature to enable a feature, or -feature to disable it.\n"
149       << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
150   }
151   exit(1);
152 }
153
154 //===----------------------------------------------------------------------===//
155 //                    SubtargetFeatures Implementation
156 //===----------------------------------------------------------------------===//
157
158 SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
159   // Break up string into separate features
160   Split(Features, Initial);
161 }
162
163
164 std::string SubtargetFeatures::getString() const {
165   return Join(Features);
166 }
167 void SubtargetFeatures::setString(const std::string &Initial) {
168   // Throw out old features
169   Features.clear();
170   // Break up string into separate features
171   Split(Features, LowercaseString(Initial));
172 }
173
174 /// setCPU - Set the CPU string.  Replaces previous setting.  Setting to "" 
175 /// clears CPU.
176 void SubtargetFeatures::setCPU(const std::string &String) {
177   Features[0] = LowercaseString(String);
178 }
179
180
181
182 /// Parse feature string for quick usage.
183 ///
184 uint32_t SubtargetFeatures::Parse(const std::string &String,
185                                   const std::string &DefaultCPU,
186                                   const SubtargetFeatureKV *CPUTable,
187                                   size_t CPUTableSize,
188                                   const SubtargetFeatureKV *FeatureTable,
189                                   size_t FeatureTableSize) {
190   assert(CPUTable && "missing CPU table");
191   assert(FeatureTable && "missing features table");
192 #ifndef NDEBUG
193   for (size_t i = 1; i < CPUTableSize; i++) {
194     assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
195            "CPU table is not sorted");
196   }
197   for (size_t i = 1; i < FeatureTableSize; i++) {
198     assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
199           "CPU features table is not sorted");
200   }
201 #endif
202   std::vector<std::string> Features;    // Subtarget features as a vector
203   uint32_t Bits = 0;                    // Resulting bits
204   // Split up features
205   Split(Features, String);
206   // Check if default is needed
207   if (Features[0].empty()) Features[0] = DefaultCPU;
208   // Check for help
209   if (Features[0] == "help") Help(false, CPUTable, CPUTableSize);
210   // Find CPU entry
211   const SubtargetFeatureKV *CPUEntry =
212                             Find(Features[0], CPUTable, CPUTableSize);
213   // If there is a match
214   if (CPUEntry) {
215     // Set base feature bits
216     Bits = CPUEntry->Value;
217   } else {
218     std::cerr << "'" << Features[0]
219               << "' is not a recognized processor for this target"
220               << " (ignoring processor)"
221               << "\n";
222   }
223   // Iterate through each feature
224   for (size_t i = 1; i < Features.size(); i++) {
225     // Get next feature
226     const std::string &Feature = Features[i];
227     // Check for help
228     if (Feature == "+help") Help(true, FeatureTable, FeatureTableSize);
229     // Find feature in table.
230     const SubtargetFeatureKV *FeatureEntry =
231                        Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
232     // If there is a match
233     if (FeatureEntry) {
234       // Enable/disable feature in bits
235       if (isEnabled(Feature)) Bits |=  FeatureEntry->Value;
236       else                    Bits &= ~FeatureEntry->Value;
237     } else {
238       std::cerr << "'" << Feature
239                 << "' is not a recognized feature for this target"
240                 << " (ignoring feature)"
241                 << "\n";
242     }
243   }
244   return Bits;
245 }
246
247 /// Print feature string.
248 void SubtargetFeatures::print(std::ostream &OS) const {
249   for (size_t i = 0; i < Features.size(); i++) {
250     OS << Features[i] << "  ";
251   }
252   OS << "\n";
253 }
254
255 /// Dump feature info.
256 void SubtargetFeatures::dump() const {
257   print(std::cerr);
258 }