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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SubtargetFeature interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 14 | #include "llvm/MC/SubtargetFeature.h" |
David Greene | 9759c30 | 2010-01-05 01:29:36 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 962bad7 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Format.h" |
Chris Lattner | e0c86af | 2009-08-23 21:41:43 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 18 | #include <algorithm> |
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> |
Nick Lewycky | 476b242 | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 21 | #include <cstdlib> |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Static Helper Functions |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | /// hasFlag - Determine if a feature has a flag; '+' or '-' |
| 29 | /// |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 30 | static inline bool hasFlag(const StringRef Feature) { |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 31 | assert(!Feature.empty() && "Empty string"); |
| 32 | // Get first character |
| 33 | char Ch = Feature[0]; |
| 34 | // Check if first character is '+' or '-' flag |
| 35 | return Ch == '+' || Ch =='-'; |
| 36 | } |
| 37 | |
| 38 | /// StripFlag - Return string stripped of flag. |
| 39 | /// |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 40 | static inline std::string StripFlag(const StringRef Feature) { |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 41 | return hasFlag(Feature) ? Feature.substr(1) : Feature; |
| 42 | } |
| 43 | |
| 44 | /// isEnabled - Return true if enable flag; '+'. |
| 45 | /// |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 46 | static inline bool isEnabled(const StringRef Feature) { |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 47 | assert(!Feature.empty() && "Empty string"); |
| 48 | // Get first character |
| 49 | char Ch = Feature[0]; |
| 50 | // Check if first character is '+' for enabled |
| 51 | return Ch == '+'; |
| 52 | } |
| 53 | |
| 54 | /// PrependFlag - Return a string with a prepended flag; '+' or '-'. |
| 55 | /// |
Francois Pichet | 24e11af | 2011-07-01 09:23:41 +0000 | [diff] [blame] | 56 | static inline std::string PrependFlag(const StringRef Feature, |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 57 | bool IsEnabled) { |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 58 | assert(!Feature.empty() && "Empty string"); |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 59 | if (hasFlag(Feature)) |
| 60 | return Feature; |
| 61 | std::string Prefix = IsEnabled ? "+" : "-"; |
| 62 | Prefix += Feature; |
Francois Pichet | 24e11af | 2011-07-01 09:23:41 +0000 | [diff] [blame] | 63 | return Prefix; |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /// Split - Splits a string of comma separated items in to a vector of strings. |
| 67 | /// |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 68 | static void Split(std::vector<std::string> &V, const StringRef S) { |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 69 | if (S.empty()) |
| 70 | return; |
| 71 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 72 | // Start at beginning of string. |
| 73 | size_t Pos = 0; |
| 74 | while (true) { |
| 75 | // Find the next comma |
| 76 | size_t Comma = S.find(',', Pos); |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 77 | // If no comma found then the rest of the string is used |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 78 | if (Comma == std::string::npos) { |
| 79 | // Add string to vector |
| 80 | V.push_back(S.substr(Pos)); |
| 81 | break; |
| 82 | } |
| 83 | // Otherwise add substring to vector |
| 84 | V.push_back(S.substr(Pos, Comma - Pos)); |
| 85 | // Advance to next item |
| 86 | Pos = Comma + 1; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// 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] | 91 | /// |
| 92 | static std::string Join(const std::vector<std::string> &V) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 93 | // Start with empty string. |
| 94 | std::string Result; |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 95 | // If the vector is not empty |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 96 | if (!V.empty()) { |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 97 | // Start with the first feature |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 98 | Result = V[0]; |
| 99 | // For each successive feature |
| 100 | for (size_t i = 1; i < V.size(); i++) { |
| 101 | // Add a comma |
| 102 | Result += ","; |
| 103 | // Add the feature |
| 104 | Result += V[i]; |
| 105 | } |
| 106 | } |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 107 | // Return the features string |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 108 | return Result; |
| 109 | } |
| 110 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 111 | /// Adding features. |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 112 | void SubtargetFeatures::AddFeature(const StringRef String, |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 113 | bool IsEnabled) { |
| 114 | // Don't add empty features |
| 115 | if (!String.empty()) { |
| 116 | // Convert to lowercase, prepend flag and add to vector |
Benjamin Kramer | 5908536 | 2011-11-06 20:37:06 +0000 | [diff] [blame] | 117 | Features.push_back(PrependFlag(String.lower(), IsEnabled)); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 121 | /// Find KV in array using binary search. |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 122 | template<typename T> const T *Find(const StringRef S, const T *A, size_t L) { |
Jeff Cohen | 9471c8a | 2006-01-26 20:41:32 +0000 | [diff] [blame] | 123 | // Make the lower bound element we're looking for |
| 124 | T KV; |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 125 | KV.Key = S.data(); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 126 | // Determine the end of the array |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 127 | const T *Hi = A + L; |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 128 | // Binary search the array |
Jeff Cohen | 9471c8a | 2006-01-26 20:41:32 +0000 | [diff] [blame] | 129 | const T *F = std::lower_bound(A, Hi, KV); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 130 | // If not found then return NULL |
Evan Cheng | 4d9af61 | 2011-07-01 16:59:30 +0000 | [diff] [blame] | 131 | if (F == Hi || StringRef(F->Key) != S) return NULL; |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 132 | // Return the found array item |
| 133 | return F; |
| 134 | } |
| 135 | |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 136 | /// getLongestEntryLength - Return the length of the longest entry in the table. |
| 137 | /// |
| 138 | static size_t getLongestEntryLength(const SubtargetFeatureKV *Table, |
| 139 | size_t Size) { |
| 140 | size_t MaxLen = 0; |
| 141 | for (size_t i = 0; i < Size; i++) |
| 142 | MaxLen = std::max(MaxLen, std::strlen(Table[i].Key)); |
| 143 | return MaxLen; |
| 144 | } |
| 145 | |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 146 | /// Display help for feature choices. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 147 | /// |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 148 | static void Help(const SubtargetFeatureKV *CPUTable, size_t CPUTableSize, |
| 149 | const SubtargetFeatureKV *FeatTable, size_t FeatTableSize) { |
| 150 | // Determine the length of the longest CPU and Feature entries. |
| 151 | unsigned MaxCPULen = getLongestEntryLength(CPUTable, CPUTableSize); |
| 152 | unsigned MaxFeatLen = getLongestEntryLength(FeatTable, FeatTableSize); |
Chris Lattner | ba76c21 | 2005-10-23 05:33:39 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 154 | // Print the CPU table. |
Chris Lattner | e0c86af | 2009-08-23 21:41:43 +0000 | [diff] [blame] | 155 | errs() << "Available CPUs for this target:\n\n"; |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 156 | for (size_t i = 0; i != CPUTableSize; i++) |
Benjamin Kramer | 962bad7 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 157 | errs() << format(" %-*s - %s.\n", |
| 158 | MaxCPULen, CPUTable[i].Key, CPUTable[i].Desc); |
| 159 | errs() << '\n'; |
| 160 | |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 161 | // Print the Feature table. |
Chris Lattner | e0c86af | 2009-08-23 21:41:43 +0000 | [diff] [blame] | 162 | errs() << "Available features for this target:\n\n"; |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 163 | for (size_t i = 0; i != FeatTableSize; i++) |
Benjamin Kramer | 962bad7 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 164 | errs() << format(" %-*s - %s.\n", |
| 165 | MaxFeatLen, FeatTable[i].Key, FeatTable[i].Desc); |
| 166 | errs() << '\n'; |
| 167 | |
Chris Lattner | e0c86af | 2009-08-23 21:41:43 +0000 | [diff] [blame] | 168 | errs() << "Use +feature to enable a feature, or -feature to disable it.\n" |
Benjamin Kramer | 962bad7 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 169 | "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; |
Nick Lewycky | 2402123 | 2010-12-19 20:42:43 +0000 | [diff] [blame] | 170 | std::exit(1); |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 173 | //===----------------------------------------------------------------------===// |
| 174 | // SubtargetFeatures Implementation |
| 175 | //===----------------------------------------------------------------------===// |
| 176 | |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 177 | SubtargetFeatures::SubtargetFeatures(const StringRef Initial) { |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 178 | // Break up string into separate features |
| 179 | Split(Features, Initial); |
| 180 | } |
| 181 | |
| 182 | |
Rafael Espindola | 3f9b9eb | 2011-07-01 04:40:50 +0000 | [diff] [blame] | 183 | std::string SubtargetFeatures::getString() const { |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 184 | return Join(Features); |
| 185 | } |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 186 | |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 187 | /// SetImpliedBits - For each feature that is (transitively) implied by this |
| 188 | /// feature, set it. |
Anton Korobeynikov | 41a0243 | 2009-05-23 19:50:50 +0000 | [diff] [blame] | 189 | /// |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 190 | static |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 191 | void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry, |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 192 | const SubtargetFeatureKV *FeatureTable, |
| 193 | size_t FeatureTableSize) { |
| 194 | for (size_t i = 0; i < FeatureTableSize; ++i) { |
| 195 | const SubtargetFeatureKV &FE = FeatureTable[i]; |
| 196 | |
| 197 | if (FeatureEntry->Value == FE.Value) continue; |
| 198 | |
| 199 | if (FeatureEntry->Implies & FE.Value) { |
| 200 | Bits |= FE.Value; |
| 201 | SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /// ClearImpliedBits - For each feature that (transitively) implies this |
| 207 | /// feature, clear it. |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 208 | /// |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 209 | static |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 210 | void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry, |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 211 | const SubtargetFeatureKV *FeatureTable, |
| 212 | size_t FeatureTableSize) { |
| 213 | for (size_t i = 0; i < FeatureTableSize; ++i) { |
| 214 | const SubtargetFeatureKV &FE = FeatureTable[i]; |
| 215 | |
| 216 | if (FeatureEntry->Value == FE.Value) continue; |
| 217 | |
| 218 | if (FE.Implies & FeatureEntry->Value) { |
| 219 | Bits &= ~FE.Value; |
| 220 | ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); |
| 221 | } |
| 222 | } |
| 223 | } |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 224 | |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 225 | /// ToggleFeature - Toggle a feature and returns the newly updated feature |
| 226 | /// bits. |
| 227 | uint64_t |
| 228 | SubtargetFeatures::ToggleFeature(uint64_t Bits, const StringRef Feature, |
| 229 | const SubtargetFeatureKV *FeatureTable, |
| 230 | size_t FeatureTableSize) { |
| 231 | // Find feature in table. |
| 232 | const SubtargetFeatureKV *FeatureEntry = |
| 233 | Find(StripFlag(Feature), FeatureTable, FeatureTableSize); |
| 234 | // If there is a match |
| 235 | if (FeatureEntry) { |
| 236 | if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) { |
| 237 | Bits &= ~FeatureEntry->Value; |
| 238 | |
| 239 | // For each feature that implies this, clear it. |
| 240 | ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); |
| 241 | } else { |
| 242 | Bits |= FeatureEntry->Value; |
| 243 | |
| 244 | // For each feature that this implies, set it. |
| 245 | SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); |
| 246 | } |
| 247 | } else { |
| 248 | errs() << "'" << Feature |
| 249 | << "' is not a recognized feature for this target" |
| 250 | << " (ignoring feature)\n"; |
| 251 | } |
| 252 | |
| 253 | return Bits; |
| 254 | } |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 255 | |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 256 | |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 257 | /// getFeatureBits - Get feature bits a CPU. |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 258 | /// |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 259 | uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU, |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 260 | const SubtargetFeatureKV *CPUTable, |
| 261 | size_t CPUTableSize, |
| 262 | const SubtargetFeatureKV *FeatureTable, |
| 263 | size_t FeatureTableSize) { |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 264 | if (!FeatureTableSize || !CPUTableSize) |
| 265 | return 0; |
| 266 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 267 | #ifndef NDEBUG |
| 268 | for (size_t i = 1; i < CPUTableSize; i++) { |
| 269 | assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 && |
| 270 | "CPU table is not sorted"); |
| 271 | } |
| 272 | for (size_t i = 1; i < FeatureTableSize; i++) { |
| 273 | assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 && |
| 274 | "CPU features table is not sorted"); |
| 275 | } |
| 276 | #endif |
Evan Cheng | b6a6388 | 2011-04-15 19:35:46 +0000 | [diff] [blame] | 277 | uint64_t Bits = 0; // Resulting bits |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 278 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 279 | // Check if help is needed |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 280 | if (CPU == "help") |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 281 | Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 282 | |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 283 | // Find CPU entry if CPU name is specified. |
| 284 | if (!CPU.empty()) { |
| 285 | const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize); |
| 286 | // If there is a match |
| 287 | if (CPUEntry) { |
| 288 | // Set base feature bits |
| 289 | Bits = CPUEntry->Value; |
Bill Wendling | 1a636de | 2007-06-27 23:34:06 +0000 | [diff] [blame] | 290 | |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 291 | // Set the feature implied by this CPU feature, if any. |
| 292 | for (size_t i = 0; i < FeatureTableSize; ++i) { |
| 293 | const SubtargetFeatureKV &FE = FeatureTable[i]; |
| 294 | if (CPUEntry->Value & FE.Value) |
| 295 | SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize); |
| 296 | } |
| 297 | } else { |
| 298 | errs() << "'" << CPU |
| 299 | << "' is not a recognized processor for this target" |
| 300 | << " (ignoring processor)\n"; |
Bill Wendling | 1a636de | 2007-06-27 23:34:06 +0000 | [diff] [blame] | 301 | } |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 302 | } |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 303 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 304 | // Iterate through each feature |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 305 | for (size_t i = 0, E = Features.size(); i < E; i++) { |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 306 | const StringRef Feature = Features[i]; |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 307 | |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 308 | // Check for help |
Chris Lattner | 3e808a4 | 2005-10-23 22:23:13 +0000 | [diff] [blame] | 309 | if (Feature == "+help") |
| 310 | Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize); |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 311 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 312 | // Find feature in table. |
| 313 | const SubtargetFeatureKV *FeatureEntry = |
| 314 | Find(StripFlag(Feature), FeatureTable, FeatureTableSize); |
| 315 | // If there is a match |
| 316 | if (FeatureEntry) { |
| 317 | // Enable/disable feature in bits |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 318 | if (isEnabled(Feature)) { |
| 319 | Bits |= FeatureEntry->Value; |
| 320 | |
| 321 | // For each feature that this implies, set it. |
| 322 | SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); |
| 323 | } else { |
| 324 | Bits &= ~FeatureEntry->Value; |
| 325 | |
| 326 | // For each feature that implies this, clear it. |
| 327 | ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize); |
| 328 | } |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 329 | } else { |
Chris Lattner | e0c86af | 2009-08-23 21:41:43 +0000 | [diff] [blame] | 330 | errs() << "'" << Feature |
| 331 | << "' is not a recognized feature for this target" |
| 332 | << " (ignoring feature)\n"; |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
Bill Wendling | 4222d80 | 2007-05-04 20:38:40 +0000 | [diff] [blame] | 335 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 336 | return Bits; |
| 337 | } |
| 338 | |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 339 | /// Get scheduling itinerary of a CPU. |
Evan Cheng | e1bff38 | 2011-07-01 00:23:10 +0000 | [diff] [blame] | 340 | void *SubtargetFeatures::getItinerary(const StringRef CPU, |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 341 | const SubtargetInfoKV *Table, |
| 342 | size_t TableSize) { |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 343 | assert(Table && "missing table"); |
| 344 | #ifndef NDEBUG |
| 345 | for (size_t i = 1; i < TableSize; i++) { |
| 346 | assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted"); |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | // Find entry |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 351 | const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize); |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 352 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 353 | if (Entry) { |
| 354 | return Entry->Value; |
| 355 | } else { |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 356 | errs() << "'" << CPU |
Chris Lattner | e0c86af | 2009-08-23 21:41:43 +0000 | [diff] [blame] | 357 | << "' is not a recognized processor for this target" |
| 358 | << " (ignoring processor)\n"; |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /// print - Print feature string. |
| 364 | /// |
Chris Lattner | e0c86af | 2009-08-23 21:41:43 +0000 | [diff] [blame] | 365 | void SubtargetFeatures::print(raw_ostream &OS) const { |
| 366 | for (size_t i = 0, e = Features.size(); i != e; ++i) |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 367 | OS << Features[i] << " "; |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 368 | OS << "\n"; |
| 369 | } |
| 370 | |
Jim Laskey | 34bd5d5 | 2005-10-25 15:15:28 +0000 | [diff] [blame] | 371 | /// dump - Dump feature info. |
| 372 | /// |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 373 | void SubtargetFeatures::dump() const { |
David Greene | 9759c30 | 2010-01-05 01:29:36 +0000 | [diff] [blame] | 374 | print(dbgs()); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 375 | } |
Viktor Kutuzov | e823db8 | 2009-11-18 20:20:05 +0000 | [diff] [blame] | 376 | |
Bill Wendling | 81043ee | 2010-05-11 00:30:02 +0000 | [diff] [blame] | 377 | /// getDefaultSubtargetFeatures - Return a string listing the features |
| 378 | /// associated with the target triple. |
Viktor Kutuzov | e823db8 | 2009-11-18 20:20:05 +0000 | [diff] [blame] | 379 | /// |
| 380 | /// FIXME: This is an inelegant way of specifying the features of a |
| 381 | /// subtarget. It would be better if we could encode this information |
| 382 | /// into the IR. See <rdar://5972456>. |
| 383 | /// |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 384 | void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) { |
Bill Wendling | f6d8481 | 2010-05-11 20:46:04 +0000 | [diff] [blame] | 385 | if (Triple.getVendor() == Triple::Apple) { |
| 386 | if (Triple.getArch() == Triple::ppc) { |
| 387 | // powerpc-apple-* |
| 388 | AddFeature("altivec"); |
| 389 | } else if (Triple.getArch() == Triple::ppc64) { |
| 390 | // powerpc64-apple-* |
| 391 | AddFeature("64bit"); |
| 392 | AddFeature("altivec"); |
Viktor Kutuzov | e823db8 | 2009-11-18 20:20:05 +0000 | [diff] [blame] | 393 | } |
Bill Wendling | 81043ee | 2010-05-11 00:30:02 +0000 | [diff] [blame] | 394 | } |
Viktor Kutuzov | e823db8 | 2009-11-18 20:20:05 +0000 | [diff] [blame] | 395 | } |