Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 1 | //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Jim Laskey | 98a6979 | 2006-03-24 10:00:56 +0000 | [diff] [blame] | 5 | // This file was developed by James M. Laskey and is distributed under the |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 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 Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Streams.h" |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Bill Wendling | 1a097e3 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 18 | #include <ostream> |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 19 | #include <cassert> |
Jeff Cohen | 7cd57f4 | 2005-09-02 02:51:42 +0000 | [diff] [blame] | 20 | #include <cctype> |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // Static Helper Functions |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | /// hasFlag - Determine if a feature has a flag; '+' or '-' |
| 28 | /// |
| 29 | static inline bool hasFlag(const std::string &Feature) { |
| 30 | assert(!Feature.empty() && "Empty string"); |
| 31 | // Get first character |
| 32 | char Ch = Feature[0]; |
| 33 | // Check if first character is '+' or '-' flag |
| 34 | return Ch == '+' || Ch =='-'; |
| 35 | } |
| 36 | |
| 37 | /// StripFlag - Return string stripped of flag. |
| 38 | /// |
| 39 | static inline std::string StripFlag(const std::string &Feature) { |
| 40 | return hasFlag(Feature) ? Feature.substr(1) : Feature; |
| 41 | } |
| 42 | |
| 43 | /// isEnabled - Return true if enable flag; '+'. |
| 44 | /// |
| 45 | static inline bool isEnabled(const std::string &Feature) { |
| 46 | assert(!Feature.empty() && "Empty string"); |
| 47 | // Get first character |
| 48 | char Ch = Feature[0]; |
| 49 | // Check if first character is '+' for enabled |
| 50 | return Ch == '+'; |
| 51 | } |
| 52 | |
| 53 | /// PrependFlag - Return a string with a prepended flag; '+' or '-'. |
| 54 | /// |
| 55 | static inline std::string PrependFlag(const std::string &Feature, |
| 56 | bool IsEnabled) { |
| 57 | assert(!Feature.empty() && "Empty string"); |
| 58 | if (hasFlag(Feature)) return Feature; |
| 59 | return std::string(IsEnabled ? "+" : "-") + Feature; |
| 60 | } |
| 61 | |
| 62 | /// Split - Splits a string of comma separated items in to a vector of strings. |
| 63 | /// |
| 64 | static void Split(std::vector<std::string> &V, const std::string &S) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 65 | // Start at beginning of string. |
| 66 | size_t Pos = 0; |
| 67 | while (true) { |
| 68 | // Find the next comma |
| 69 | size_t Comma = S.find(',', Pos); |
| 70 | // If no comma found then the the rest of the string is used |
| 71 | if (Comma == std::string::npos) { |
| 72 | // Add string to vector |
| 73 | V.push_back(S.substr(Pos)); |
| 74 | break; |
| 75 | } |
| 76 | // Otherwise add substring to vector |
| 77 | V.push_back(S.substr(Pos, Comma - Pos)); |
| 78 | // Advance to next item |
| 79 | Pos = Comma + 1; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Join a vector of strings to a string with a comma separating each element. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 84 | /// |
| 85 | static std::string Join(const std::vector<std::string> &V) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 86 | // Start with empty string. |
| 87 | std::string Result; |
| 88 | // If the vector is not empty |
| 89 | if (!V.empty()) { |
| 90 | // Start with the CPU feature |
| 91 | Result = V[0]; |
| 92 | // For each successive feature |
| 93 | for (size_t i = 1; i < V.size(); i++) { |
| 94 | // Add a comma |
| 95 | Result += ","; |
| 96 | // Add the feature |
| 97 | Result += V[i]; |
| 98 | } |
| 99 | } |
| 100 | // Return the features string |
| 101 | return Result; |
| 102 | } |
| 103 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 104 | /// Adding features. |
| 105 | void SubtargetFeatures::AddFeature(const std::string &String, |
| 106 | bool IsEnabled) { |
| 107 | // Don't add empty features |
| 108 | if (!String.empty()) { |
| 109 | // Convert to lowercase, prepend flag and add to vector |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 110 | Features.push_back(PrependFlag(LowercaseString(String), IsEnabled)); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 114 | /// Find KV in array using binary search. |
| 115 | template<typename T> const T *Find(const std::string &S, const T *A, size_t L) { |
Jeff Cohen | 9471c8a | 2006-01-26 20:41:32 +0000 | [diff] [blame] | 116 | // Make the lower bound element we're looking for |
| 117 | T KV; |
| 118 | KV.Key = S.c_str(); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 119 | // Determine the end of the array |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 120 | const T *Hi = A + L; |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 121 | // Binary search the array |
Jeff Cohen | 9471c8a | 2006-01-26 20:41:32 +0000 | [diff] [blame] | 122 | const T *F = std::lower_bound(A, Hi, KV); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 123 | // If not found then return NULL |
| 124 | if (F == Hi || std::string(F->Key) != S) return NULL; |
| 125 | // Return the found array item |
| 126 | return F; |
| 127 | } |
| 128 | |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 129 | /// getLongestEntryLength - Return the length of the longest entry in the table. |
| 130 | /// |
| 131 | static size_t getLongestEntryLength(const SubtargetFeatureKV *Table, |
| 132 | size_t Size) { |
| 133 | size_t MaxLen = 0; |
| 134 | for (size_t i = 0; i < Size; i++) |
| 135 | MaxLen = std::max(MaxLen, std::strlen(Table[i].Key)); |
| 136 | return MaxLen; |
| 137 | } |
| 138 | |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 139 | /// Display help for feature choices. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 140 | /// |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 141 | static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize, |
| 142 | const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) { |
| 143 | // Determine the length of the longest CPU and Feature entries. |
| 144 | unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize); |
| 145 | unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize); |
Chris Lattner | ba76c21 | 2005-10-23 05:33:39 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 147 | // Print the CPU table. |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 148 | cerr << "Available CPUs for this target:\n\n"; |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 149 | for (size_t i = 0; i != CPUTableSize; i++) |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 150 | cerr << " " << CPUTable[i].Key |
| 151 | << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ') |
| 152 | << " - " << CPUTable[i].Desc << ".\n"; |
| 153 | cerr << "\n"; |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 154 | |
| 155 | // Print the Feature table. |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 156 | cerr << "Available features for this target:\n\n"; |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 157 | for (size_t i = 0; i != FeatTableSize; i++) |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 158 | cerr << " " << FeatTable[i].Key |
| 159 | << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ') |
| 160 | << " - " << FeatTable[i].Desc << ".\n"; |
| 161 | cerr << "\n"; |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 162 | |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 163 | cerr << "Use +feature to enable a feature, or -feature to disable it.\n" |
| 164 | << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 165 | exit(1); |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
| 169 | // SubtargetFeatures Implementation |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
| 172 | SubtargetFeatures::SubtargetFeatures(const std::string &Initial) { |
| 173 | // Break up string into separate features |
| 174 | Split(Features, Initial); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | std::string SubtargetFeatures::getString() const { |
| 179 | return Join(Features); |
| 180 | } |
| 181 | void SubtargetFeatures::setString(const std::string &Initial) { |
| 182 | // Throw out old features |
| 183 | Features.clear(); |
| 184 | // Break up string into separate features |
| 185 | Split(Features, LowercaseString(Initial)); |
| 186 | } |
| 187 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 189 | /// setCPU - Set the CPU string. Replaces previous setting. Setting to "" |
| 190 | /// clears CPU. |
| 191 | void SubtargetFeatures::setCPU(const std::string &String) { |
| 192 | Features[0] = LowercaseString(String); |
| 193 | } |
| 194 | |
| 195 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 196 | /// setCPUIfNone - Setting CPU string only if no string is set. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 197 | /// |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 198 | void SubtargetFeatures::setCPUIfNone(const std::string &String) { |
| 199 | if (Features[0].empty()) setCPU(String); |
| 200 | } |
| 201 | |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 202 | /// SetImpliedBits - For each feature that is (transitively) implied by this |
| 203 | /// feature, set it. |
| 204 | /// |
| 205 | static |
| 206 | void SetImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry, |
| 207 | const SubtargetFeatureKV *FeatureTable, |
| 208 | size_t FeatureTableSize) { |
| 209 | for (size_t i = 0; i < FeatureTableSize; ++i) { |
| 210 | const SubtargetFeatureKV &FE = FeatureTable[i]; |
| 211 | |
| 212 | if (FeatureEntry->Value == FE.Value) continue; |
| 213 | |
| 214 | if (FeatureEntry->Implies & FE.Value) { |
| 215 | Bits |= FE.Value; |
| 216 | SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// ClearImpliedBits - For each feature that (transitively) implies this |
| 222 | /// feature, clear it. |
| 223 | /// |
| 224 | static |
| 225 | void ClearImpliedBits(uint32_t &Bits, const SubtargetFeatureKV *FeatureEntry, |
| 226 | const SubtargetFeatureKV *FeatureTable, |
| 227 | size_t FeatureTableSize) { |
| 228 | for (size_t i = 0; i < FeatureTableSize; ++i) { |
| 229 | const SubtargetFeatureKV &FE = FeatureTable[i]; |
| 230 | |
| 231 | if (FeatureEntry->Value == FE.Value) continue; |
| 232 | |
| 233 | if (FE.Implies & FeatureEntry->Value) { |
| 234 | Bits &= ~FE.Value; |
| 235 | ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); |
| 236 | } |
| 237 | } |
| 238 | } |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 239 | |
| 240 | /// getBits - Get feature bits. |
| 241 | /// |
| 242 | uint32_t SubtargetFeatures::getBits(const SubtargetFeatureKV *CPUTable, |
| 243 | size_t CPUTableSize, |
| 244 | const SubtargetFeatureKV *FeatureTable, |
| 245 | size_t FeatureTableSize) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 246 | assert(CPUTable && "missing CPU table"); |
| 247 | assert(FeatureTable && "missing features table"); |
| 248 | #ifndef NDEBUG |
| 249 | for (size_t i = 1; i < CPUTableSize; i++) { |
| 250 | assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 && |
| 251 | "CPU table is not sorted"); |
| 252 | } |
| 253 | for (size_t i = 1; i < FeatureTableSize; i++) { |
| 254 | assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 && |
| 255 | "CPU features table is not sorted"); |
| 256 | } |
| 257 | #endif |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 258 | uint32_t Bits = 0; // Resulting bits |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 259 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 260 | // Check if help is needed |
| 261 | if (Features[0] == "help") |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 262 | Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); |
| 263 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 264 | // Find CPU entry |
| 265 | const SubtargetFeatureKV *CPUEntry = |
| 266 | Find(Features[0], CPUTable, CPUTableSize); |
| 267 | // If there is a match |
| 268 | if (CPUEntry) { |
| 269 | // Set base feature bits |
| 270 | Bits = CPUEntry->Value; |
Bill Wendling | 1a636de | 2007-06-27 23:34:06 +0000 | [diff] [blame] | 271 | |
| 272 | // Set the feature implied by this CPU feature, if any. |
| 273 | for (size_t i = 0; i < FeatureTableSize; ++i) { |
| 274 | const SubtargetFeatureKV &FE = FeatureTable[i]; |
| 275 | if (CPUEntry->Value & FE.Value) |
| 276 | SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); |
| 277 | } |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 278 | } else { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 279 | cerr << "'" << Features[0] |
| 280 | << "' is not a recognized processor for this target" |
| 281 | << " (ignoring processor)" |
| 282 | << "\n"; |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 283 | } |
| 284 | // Iterate through each feature |
| 285 | for (size_t i = 1; i < Features.size(); i++) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 286 | const std::string &Feature = Features[i]; |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 287 | |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 288 | // Check for help |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 289 | if (Feature == "+help") |
| 290 | Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); |
| 291 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 292 | // Find feature in table. |
| 293 | const SubtargetFeatureKV *FeatureEntry = |
| 294 | Find(StripFlag(Feature), FeatureTable, FeatureTableSize); |
| 295 | // If there is a match |
| 296 | if (FeatureEntry) { |
| 297 | // Enable/disable feature in bits |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 298 | if (isEnabled(Feature)) { |
| 299 | Bits |= FeatureEntry->Value; |
| 300 | |
| 301 | // For each feature that this implies, set it. |
| 302 | SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); |
| 303 | } else { |
| 304 | Bits &= ~FeatureEntry->Value; |
| 305 | |
| 306 | // For each feature that implies this, clear it. |
| 307 | ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); |
| 308 | } |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 309 | } else { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 310 | cerr << "'" << Feature |
| 311 | << "' is not a recognized feature for this target" |
| 312 | << " (ignoring feature)" |
| 313 | << "\n"; |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 316 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 317 | return Bits; |
| 318 | } |
| 319 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 320 | /// Get info pointer |
| 321 | void *SubtargetFeatures::getInfo(const SubtargetInfoKV *Table, |
| 322 | size_t TableSize) { |
| 323 | assert(Table && "missing table"); |
| 324 | #ifndef NDEBUG |
| 325 | for (size_t i = 1; i < TableSize; i++) { |
| 326 | assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted"); |
| 327 | } |
| 328 | #endif |
| 329 | |
| 330 | // Find entry |
| 331 | const SubtargetInfoKV *Entry = Find(Features[0], Table, TableSize); |
| 332 | |
| 333 | if (Entry) { |
| 334 | return Entry->Value; |
| 335 | } else { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 336 | cerr << "'" << Features[0] |
| 337 | << "' is not a recognized processor for this target" |
| 338 | << " (ignoring processor)" |
| 339 | << "\n"; |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /// print - Print feature string. |
| 345 | /// |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 346 | void SubtargetFeatures::print(std::ostream &OS) const { |
| 347 | for (size_t i = 0; i < Features.size(); i++) { |
| 348 | OS << Features[i] << " "; |
| 349 | } |
| 350 | OS << "\n"; |
| 351 | } |
| 352 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 353 | /// dump - Dump feature info. |
| 354 | /// |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 355 | void SubtargetFeatures::dump() const { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 356 | print(*cerr.stream()); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 357 | } |