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