LLVM API Documentation
00001 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the SubtargetFeature interface. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Target/SubtargetFeature.h" 00015 #include "llvm/ADT/StringExtras.h" 00016 #include "llvm/Support/Streams.h" 00017 #include <algorithm> 00018 #include <ostream> 00019 #include <cassert> 00020 #include <cctype> 00021 using namespace llvm; 00022 00023 //===----------------------------------------------------------------------===// 00024 // Static Helper Functions 00025 //===----------------------------------------------------------------------===// 00026 00027 /// hasFlag - Determine if a feature has a flag; '+' or '-' 00028 /// 00029 static inline bool hasFlag(const std::string &Feature) { 00030 assert(!Feature.empty() && "Empty string"); 00031 // Get first character 00032 char Ch = Feature[0]; 00033 // Check if first character is '+' or '-' flag 00034 return Ch == '+' || Ch =='-'; 00035 } 00036 00037 /// StripFlag - Return string stripped of flag. 00038 /// 00039 static inline std::string StripFlag(const std::string &Feature) { 00040 return hasFlag(Feature) ? Feature.substr(1) : Feature; 00041 } 00042 00043 /// isEnabled - Return true if enable flag; '+'. 00044 /// 00045 static inline bool isEnabled(const std::string &Feature) { 00046 assert(!Feature.empty() && "Empty string"); 00047 // Get first character 00048 char Ch = Feature[0]; 00049 // Check if first character is '+' for enabled 00050 return Ch == '+'; 00051 } 00052 00053 /// PrependFlag - Return a string with a prepended flag; '+' or '-'. 00054 /// 00055 static inline std::string PrependFlag(const std::string &Feature, 00056 bool IsEnabled) { 00057 assert(!Feature.empty() && "Empty string"); 00058 if (hasFlag(Feature)) return Feature; 00059 return std::string(IsEnabled ? "+" : "-") + Feature; 00060 } 00061 00062 /// Split - Splits a string of comma separated items in to a vector of strings. 00063 /// 00064 static void Split(std::vector<std::string> &V, const std::string &S) { 00065 // Start at beginning of string. 00066 size_t Pos = 0; 00067 while (true) { 00068 // Find the next comma 00069 size_t Comma = S.find(',', Pos); 00070 // If no comma found then the the rest of the string is used 00071 if (Comma == std::string::npos) { 00072 // Add string to vector 00073 V.push_back(S.substr(Pos)); 00074 break; 00075 } 00076 // Otherwise add substring to vector 00077 V.push_back(S.substr(Pos, Comma - Pos)); 00078 // Advance to next item 00079 Pos = Comma + 1; 00080 } 00081 } 00082 00083 /// Join a vector of strings to a string with a comma separating each element. 00084 /// 00085 static std::string Join(const std::vector<std::string> &V) { 00086 // Start with empty string. 00087 std::string Result; 00088 // If the vector is not empty 00089 if (!V.empty()) { 00090 // Start with the CPU feature 00091 Result = V[0]; 00092 // For each successive feature 00093 for (size_t i = 1; i < V.size(); i++) { 00094 // Add a comma 00095 Result += ","; 00096 // Add the feature 00097 Result += V[i]; 00098 } 00099 } 00100 // Return the features string 00101 return Result; 00102 } 00103 00104 /// Adding features. 00105 void SubtargetFeatures::AddFeature(const std::string &String, 00106 bool IsEnabled) { 00107 // Don't add empty features 00108 if (!String.empty()) { 00109 // Convert to lowercase, prepend flag and add to vector 00110 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled)); 00111 } 00112 } 00113 00114 /// Find KV in array using binary search. 00115 template<typename T> const T *Find(const std::string &S, const T *A, size_t L) { 00116 // Make the lower bound element we're looking for 00117 T KV; 00118 KV.Key = S.c_str(); 00119 // Determine the end of the array 00120 const T *Hi = A + L; 00121 // Binary search the array 00122 const T *F = std::lower_bound(A, Hi, KV); 00123 // If not found then return NULL 00124 if (F == Hi || std::string(F->Key) != S) return NULL; 00125 // Return the found array item 00126 return F; 00127 } 00128 00129 /// getLongestEntryLength - Return the length of the longest entry in the table. 00130 /// 00131 static size_t getLongestEntryLength(const SubtargetFeatureKV *Table, 00132 size_t Size) { 00133 size_t MaxLen = 0; 00134 for (size_t i = 0; i < Size; i++) 00135 MaxLen = std::max(MaxLen, std::strlen(Table[i].Key)); 00136 return MaxLen; 00137 } 00138 00139 /// Display help for feature choices. 00140 /// 00141 static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize, 00142 const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) { 00143 // Determine the length of the longest CPU and Feature entries. 00144 unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize); 00145 unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize); 00146 00147 // Print the CPU table. 00148 cerr << "Available CPUs for this target:\n\n"; 00149 for (size_t i = 0; i != CPUTableSize; i++) 00150 cerr << " " << CPUTable[i].Key 00151 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ') 00152 << " - " << CPUTable[i].Desc << ".\n"; 00153 cerr << "\n"; 00154 00155 // Print the Feature table. 00156 cerr << "Available features for this target:\n\n"; 00157 for (size_t i = 0; i != FeatTableSize; i++) 00158 cerr << " " << FeatTable[i].Key 00159 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ') 00160 << " - " << FeatTable[i].Desc << ".\n"; 00161 cerr << "\n"; 00162 00163 cerr << "Use +feature to enable a feature, or -feature to disable it.\n" 00164 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; 00165 exit(1); 00166 } 00167 00168 //===----------------------------------------------------------------------===// 00169 // SubtargetFeatures Implementation 00170 //===----------------------------------------------------------------------===// 00171 00172 SubtargetFeatures::SubtargetFeatures(const std::string &Initial) { 00173 // Break up string into separate features 00174 Split(Features, Initial); 00175 } 00176 00177 00178 std::string SubtargetFeatures::getString() const { 00179 return Join(Features); 00180 } 00181 void SubtargetFeatures::setString(const std::string &Initial) { 00182 // Throw out old features 00183 Features.clear(); 00184 // Break up string into separate features 00185 Split(Features, LowercaseString(Initial)); 00186 } 00187 00188 00189 /// setCPU - Set the CPU string. Replaces previous setting. Setting to "" 00190 /// clears CPU. 00191 void SubtargetFeatures::setCPU(const std::string &String) { 00192 Features[0] = LowercaseString(String); 00193 } 00194 00195 00196 /// setCPUIfNone - Setting CPU string only if no string is set. 00197 /// 00198 void SubtargetFeatures::setCPUIfNone(const std::string &String) { 00199 if (Features[0].empty()) setCPU(String); 00200 } 00201 00202 /// getCPU - Returns current CPU. 00203 /// 00204 const std::string & SubtargetFeatures::getCPU() const { 00205 return Features[0]; 00206 } 00207 00208 00209 /// SetImpliedBits - For each feature that is (transitively) implied by this 00210 /// feature, set it. 00211 /// 00212 static 00213 void SetImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry, 00214 const SubtargetFeatureKV *FeatureTable, 00215 size_t FeatureTableSize) { 00216 for (size_t i = 0; i < FeatureTableSize; ++i) { 00217 const SubtargetFeatureKV &FE = FeatureTable[i]; 00218 00219 if (FeatureEntry->Value == FE.Value) continue; 00220 00221 if (FeatureEntry->Implies & FE.Value) { 00222 Bits |= FE.Value; 00223 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); 00224 } 00225 } 00226 } 00227 00228 /// ClearImpliedBits - For each feature that (transitively) implies this 00229 /// feature, clear it. 00230 /// 00231 static 00232 void ClearImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry, 00233 const SubtargetFeatureKV *FeatureTable, 00234 size_t FeatureTableSize) { 00235 for (size_t i = 0; i < FeatureTableSize; ++i) { 00236 const SubtargetFeatureKV &FE = FeatureTable[i]; 00237 00238 if (FeatureEntry->Value == FE.Value) continue; 00239 00240 if (FE.Implies & FeatureEntry->Value) { 00241 Bits &= ~FE.Value; 00242 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); 00243 } 00244 } 00245 } 00246 00247 /// getBits - Get feature bits. 00248 /// 00249 uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable, 00250 size_t CPUTableSize, 00251 const SubtargetFeatureKV *FeatureTable, 00252 size_t FeatureTableSize) { 00253 assert(CPUTable && "missing CPU table"); 00254 assert(FeatureTable && "missing features table"); 00255 #ifndef NDEBUG 00256 for (size_t i = 1; i < CPUTableSize; i++) { 00257 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 && 00258 "CPU table is not sorted"); 00259 } 00260 for (size_t i = 1; i < FeatureTableSize; i++) { 00261 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 && 00262 "CPU features table is not sorted"); 00263 } 00264 #endif 00265 uint32_t Bits = 0; // Resulting bits 00266 00267 // Check if help is needed 00268 if (Features[0] == "help") 00269 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); 00270 00271 // Find CPU entry 00272 const SubtargetFeatureKV *CPUEntry = 00273 Find(Features[0], CPUTable, CPUTableSize); 00274 // If there is a match 00275 if (CPUEntry) { 00276 // Set base feature bits 00277 Bits = CPUEntry->Value; 00278 00279 // Set the feature implied by this CPU feature, if any. 00280 for (size_t i = 0; i < FeatureTableSize; ++i) { 00281 const SubtargetFeatureKV &FE = FeatureTable[i]; 00282 if (CPUEntry->Value & FE.Value) 00283 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); 00284 } 00285 } else { 00286 cerr << "'" << Features[0] 00287 << "' is not a recognized processor for this target" 00288 << " (ignoring processor)" 00289 << "\n"; 00290 } 00291 // Iterate through each feature 00292 for (size_t i = 1; i < Features.size(); i++) { 00293 const std::string &Feature = Features[i]; 00294 00295 // Check for help 00296 if (Feature == "+help") 00297 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); 00298 00299 // Find feature in table. 00300 const SubtargetFeatureKV *FeatureEntry = 00301 Find(StripFlag(Feature), FeatureTable, FeatureTableSize); 00302 // If there is a match 00303 if (FeatureEntry) { 00304 // Enable/disable feature in bits 00305 if (isEnabled(Feature)) { 00306 Bits |= FeatureEntry->Value; 00307 00308 // For each feature that this implies, set it. 00309 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); 00310 } else { 00311 Bits &= ~FeatureEntry->Value; 00312 00313 // For each feature that implies this, clear it. 00314 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); 00315 } 00316 } else { 00317 cerr << "'" << Feature 00318 << "' is not a recognized feature for this target" 00319 << " (ignoring feature)" 00320 << "\n"; 00321 } 00322 } 00323 00324 return Bits; 00325 } 00326 00327 /// Get info pointer 00328 void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table, 00329 size_t TableSize) { 00330 assert(Table && "missing table"); 00331 #ifndef NDEBUG 00332 for (size_t i = 1; i < TableSize; i++) { 00333 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted"); 00334 } 00335 #endif 00336 00337 // Find entry 00338 const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize); 00339 00340 if (Entry) { 00341 return Entry->Value; 00342 } else { 00343 cerr << "'" << Features[0] 00344 << "' is not a recognized processor for this target" 00345 << " (ignoring processor)" 00346 << "\n"; 00347 return NULL; 00348 } 00349 } 00350 00351 /// print - Print feature string. 00352 /// 00353 void SubtargetFeatures::print(std::ostream &OS) const { 00354 for (size_t i = 0; i < Features.size(); i++) { 00355 OS << Features[i] << " "; 00356 } 00357 OS << "\n"; 00358 } 00359 00360 /// dump - Dump feature info. 00361 /// 00362 void SubtargetFeatures::dump() const { 00363 print(*cerr.stream()); 00364 }