kill SelectCALL() in the DAG isel, we handle this in lowering now, like
[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 KV in array using binary search.
114 template<typename T> const T *Find(const std::string &S, const T *A, size_t L) {
115   // Determine the end of the array
116   const T *Hi = A + L;
117   // Binary search the array
118   const T *F = std::lower_bound(A, Hi, S);
119   // If not found then return NULL
120   if (F == Hi || std::string(F->Key) != S) return NULL;
121   // Return the found array item
122   return F;
123 }
124
125 /// getLongestEntryLength - Return the length of the longest entry in the table.
126 ///
127 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table,
128                                     size_t Size) {
129   size_t MaxLen = 0;
130   for (size_t i = 0; i < Size; i++)
131     MaxLen = std::max(MaxLen, std::strlen(Table[i].Key));
132   return MaxLen;
133 }
134
135 /// Display help for feature choices.
136 ///
137 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize,
138                  const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) {
139   // Determine the length of the longest CPU and Feature entries.
140   unsigned MaxCPULen  = getLongestEntryLength(CPUTable, CPUTableSize);
141   unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize);
142
143   // Print the CPU table.
144   std::cerr << "Available CPUs for this target:\n\n";
145   for (size_t i = 0; i != CPUTableSize; i++)
146     std::cerr << "  " << CPUTable[i].Key
147               << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
148               << " - " << CPUTable[i].Desc << ".\n";
149   std::cerr << "\n";
150   
151   // Print the Feature table.
152   std::cerr << "Available features for this target:\n\n";
153   for (size_t i = 0; i != FeatTableSize; i++)
154     std::cerr << "  " << FeatTable[i].Key
155       << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
156       << " - " << FeatTable[i].Desc << ".\n";
157   std::cerr << "\n";
158   
159   std::cerr << "Use +feature to enable a feature, or -feature to disable it.\n"
160             << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
161   exit(1);
162 }
163
164 //===----------------------------------------------------------------------===//
165 //                    SubtargetFeatures Implementation
166 //===----------------------------------------------------------------------===//
167
168 SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
169   // Break up string into separate features
170   Split(Features, Initial);
171 }
172
173
174 std::string SubtargetFeatures::getString() const {
175   return Join(Features);
176 }
177 void SubtargetFeatures::setString(const std::string &Initial) {
178   // Throw out old features
179   Features.clear();
180   // Break up string into separate features
181   Split(Features, LowercaseString(Initial));
182 }
183
184
185 /// setCPU - Set the CPU string.  Replaces previous setting.  Setting to "" 
186 /// clears CPU.
187 void SubtargetFeatures::setCPU(const std::string &String) {
188   Features[0] = LowercaseString(String);
189 }
190
191
192 /// setCPUIfNone - Setting CPU string only if no string is set.
193 ///
194 void SubtargetFeatures::setCPUIfNone(const std::string &String) {
195   if (Features[0].empty()) setCPU(String);
196 }
197
198
199 /// getBits - Get feature bits.
200 ///
201 uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable,
202                                           size_t CPUTableSize,
203                                     const SubtargetFeatureKV *FeatureTable,
204                                           size_t FeatureTableSize) {
205   assert(CPUTable && "missing CPU table");
206   assert(FeatureTable && "missing features table");
207 #ifndef NDEBUG
208   for (size_t i = 1; i < CPUTableSize; i++) {
209     assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
210            "CPU table is not sorted");
211   }
212   for (size_t i = 1; i < FeatureTableSize; i++) {
213     assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
214           "CPU features table is not sorted");
215   }
216 #endif
217   uint32_t Bits = 0;                    // Resulting bits
218
219   // Check if help is needed
220   if (Features[0] == "help")
221     Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
222   
223   // Find CPU entry
224   const SubtargetFeatureKV *CPUEntry =
225                             Find(Features[0], CPUTable, CPUTableSize);
226   // If there is a match
227   if (CPUEntry) {
228     // Set base feature bits
229     Bits = CPUEntry->Value;
230   } else {
231     std::cerr << "'" << Features[0]
232               << "' is not a recognized processor for this target"
233               << " (ignoring processor)"
234               << "\n";
235   }
236   // Iterate through each feature
237   for (size_t i = 1; i < Features.size(); i++) {
238     const std::string &Feature = Features[i];
239     
240     // Check for help
241     if (Feature == "+help")
242       Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
243     
244     // Find feature in table.
245     const SubtargetFeatureKV *FeatureEntry =
246                        Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
247     // If there is a match
248     if (FeatureEntry) {
249       // Enable/disable feature in bits
250       if (isEnabled(Feature)) Bits |=  FeatureEntry->Value;
251       else                    Bits &= ~FeatureEntry->Value;
252     } else {
253       std::cerr << "'" << Feature
254                 << "' is not a recognized feature for this target"
255                 << " (ignoring feature)"
256                 << "\n";
257     }
258   }
259   return Bits;
260 }
261
262 /// Get info pointer
263 void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table,
264                                        size_t TableSize) {
265   assert(Table && "missing table");
266 #ifndef NDEBUG
267   for (size_t i = 1; i < TableSize; i++) {
268     assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
269   }
270 #endif
271
272   // Find entry
273   const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize);
274   
275   if (Entry) {
276     return Entry->Value;
277   } else {
278     std::cerr << "'" << Features[0]
279               << "' is not a recognized processor for this target"
280               << " (ignoring processor)"
281               << "\n";
282     return NULL;
283   }
284 }
285
286 /// print - Print feature string.
287 ///
288 void SubtargetFeatures::print(std::ostream &OS) const {
289   for (size_t i = 0; i < Features.size(); i++) {
290     OS << Features[i] << "  ";
291   }
292   OS << "\n";
293 }
294
295 /// dump - Dump feature info.
296 ///
297 void SubtargetFeatures::dump() const {
298   print(std::cerr);
299 }