blob: 951e0aa5b604b742f00afc2d1c7470756b0030ab [file] [log] [blame]
Jim Laskeyb3302db2005-09-01 21:36:18 +00001//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeyb3302db2005-09-01 21:36:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SubtargetFeature interface.
11//
12//===----------------------------------------------------------------------===//
13
Evan Chengab8be962011-06-29 01:14:12 +000014#include "llvm/MC/SubtargetFeature.h"
David Greene9759c302010-01-05 01:29:36 +000015#include "llvm/Support/Debug.h"
Chris Lattnere0c86af2009-08-23 21:41:43 +000016#include "llvm/Support/raw_ostream.h"
Chris Lattner54195c12005-10-23 05:26:26 +000017#include "llvm/ADT/StringExtras.h"
Jim Laskeyb3302db2005-09-01 21:36:18 +000018#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000019#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000020#include <cctype>
Nick Lewycky476b2422010-12-19 20:43:38 +000021#include <cstdlib>
Jim Laskeyb3302db2005-09-01 21:36:18 +000022using namespace llvm;
23
Chris Lattner54195c12005-10-23 05:26:26 +000024//===----------------------------------------------------------------------===//
25// Static Helper Functions
26//===----------------------------------------------------------------------===//
27
28/// hasFlag - Determine if a feature has a flag; '+' or '-'
29///
Evan Chenge1bff382011-07-01 00:23:10 +000030static inline bool hasFlag(const StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000031 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 Chenge1bff382011-07-01 00:23:10 +000040static inline std::string StripFlag(const StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000041 return hasFlag(Feature) ? Feature.substr(1) : Feature;
42}
43
44/// isEnabled - Return true if enable flag; '+'.
45///
Evan Chenge1bff382011-07-01 00:23:10 +000046static inline bool isEnabled(const StringRef Feature) {
Chris Lattner54195c12005-10-23 05:26:26 +000047 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 Pichet24e11af2011-07-01 09:23:41 +000056static inline std::string PrependFlag(const StringRef Feature,
Evan Chenge1bff382011-07-01 00:23:10 +000057 bool IsEnabled) {
Chris Lattner54195c12005-10-23 05:26:26 +000058 assert(!Feature.empty() && "Empty string");
Evan Chenge1bff382011-07-01 00:23:10 +000059 if (hasFlag(Feature))
60 return Feature;
61 std::string Prefix = IsEnabled ? "+" : "-";
62 Prefix += Feature;
Francois Pichet24e11af2011-07-01 09:23:41 +000063 return Prefix;
Chris Lattner54195c12005-10-23 05:26:26 +000064}
65
66/// Split - Splits a string of comma separated items in to a vector of strings.
67///
Evan Chenge1bff382011-07-01 00:23:10 +000068static void Split(std::vector<std::string> &V, const StringRef S) {
Evan Cheng276365d2011-06-30 01:53:36 +000069 if (S.empty())
70 return;
71
Jim Laskeyb3302db2005-09-01 21:36:18 +000072 // 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 Gohmanf451cb82010-02-10 16:03:48 +000077 // If no comma found then the rest of the string is used
Jim Laskeyb3302db2005-09-01 21:36:18 +000078 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 Lattner54195c12005-10-23 05:26:26 +000091///
92static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000093 // Start with empty string.
94 std::string Result;
95 // If the vector is not empty
96 if (!V.empty()) {
Evan Cheng276365d2011-06-30 01:53:36 +000097 // Start with the first feature
Jim Laskeyb3302db2005-09-01 21:36:18 +000098 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 }
107 // Return the features string
108 return Result;
109}
110
Jim Laskeyb3302db2005-09-01 21:36:18 +0000111/// Adding features.
Evan Chenge1bff382011-07-01 00:23:10 +0000112void SubtargetFeatures::AddFeature(const StringRef String,
Jim Laskeyb3302db2005-09-01 21:36:18 +0000113 bool IsEnabled) {
114 // Don't add empty features
115 if (!String.empty()) {
116 // Convert to lowercase, prepend flag and add to vector
Chris Lattner54195c12005-10-23 05:26:26 +0000117 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000118 }
119}
120
Jim Laskey34bd5d52005-10-25 15:15:28 +0000121/// Find KV in array using binary search.
Evan Chenge1bff382011-07-01 00:23:10 +0000122template<typename T> const T *Find(const StringRef S, const T *A, size_t L) {
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000123 // Make the lower bound element we're looking for
124 T KV;
Evan Chenge1bff382011-07-01 00:23:10 +0000125 KV.Key = S.data();
Jim Laskeyb3302db2005-09-01 21:36:18 +0000126 // Determine the end of the array
Jim Laskey34bd5d52005-10-25 15:15:28 +0000127 const T *Hi = A + L;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000128 // Binary search the array
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000129 const T *F = std::lower_bound(A, Hi, KV);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000130 // If not found then return NULL
Evan Cheng4d9af612011-07-01 16:59:30 +0000131 if (F == Hi || StringRef(F->Key) != S) return NULL;
Jim Laskeyb3302db2005-09-01 21:36:18 +0000132 // Return the found array item
133 return F;
134}
135
Chris Lattner3e808a42005-10-23 22:23:13 +0000136/// getLongestEntryLength - Return the length of the longest entry in the table.
137///
138static 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 Laskey839615a2005-09-02 19:27:43 +0000146/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000147///
Chris Lattner3e808a42005-10-23 22:23:13 +0000148static 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 Lattnerba76c212005-10-23 05:33:39 +0000153
Chris Lattner3e808a42005-10-23 22:23:13 +0000154 // Print the CPU table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000155 errs() << "Available CPUs for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000156 for (size_t i = 0; i != CPUTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000157 errs() << " " << CPUTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000158 << std::string(MaxCPULen - std::strlen(CPUTable[i].Key), ' ')
159 << " - " << CPUTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000160 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000161
162 // Print the Feature table.
Chris Lattnere0c86af2009-08-23 21:41:43 +0000163 errs() << "Available features for this target:\n\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000164 for (size_t i = 0; i != FeatTableSize; i++)
Chris Lattnere0c86af2009-08-23 21:41:43 +0000165 errs() << " " << FeatTable[i].Key
Bill Wendlingf5da1332006-12-07 22:21:48 +0000166 << std::string(MaxFeatLen - std::strlen(FeatTable[i].Key), ' ')
167 << " - " << FeatTable[i].Desc << ".\n";
Chris Lattnere0c86af2009-08-23 21:41:43 +0000168 errs() << "\n";
Chris Lattner3e808a42005-10-23 22:23:13 +0000169
Chris Lattnere0c86af2009-08-23 21:41:43 +0000170 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +0000171 << "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
Nick Lewycky24021232010-12-19 20:42:43 +0000172 std::exit(1);
Jim Laskey839615a2005-09-02 19:27:43 +0000173}
174
Chris Lattner54195c12005-10-23 05:26:26 +0000175//===----------------------------------------------------------------------===//
176// SubtargetFeatures Implementation
177//===----------------------------------------------------------------------===//
178
Evan Chenge1bff382011-07-01 00:23:10 +0000179SubtargetFeatures::SubtargetFeatures(const StringRef Initial) {
Chris Lattner54195c12005-10-23 05:26:26 +0000180 // Break up string into separate features
181 Split(Features, Initial);
182}
183
184
Rafael Espindola3f9b9eb2011-07-01 04:40:50 +0000185std::string SubtargetFeatures::getString() const {
Chris Lattner54195c12005-10-23 05:26:26 +0000186 return Join(Features);
187}
Chris Lattner54195c12005-10-23 05:26:26 +0000188
Bill Wendling4222d802007-05-04 20:38:40 +0000189/// SetImpliedBits - For each feature that is (transitively) implied by this
190/// feature, set it.
Anton Korobeynikov41a02432009-05-23 19:50:50 +0000191///
Bill Wendling4222d802007-05-04 20:38:40 +0000192static
Evan Chengb6a63882011-04-15 19:35:46 +0000193void SetImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendling4222d802007-05-04 20:38:40 +0000194 const SubtargetFeatureKV *FeatureTable,
195 size_t FeatureTableSize) {
196 for (size_t i = 0; i < FeatureTableSize; ++i) {
197 const SubtargetFeatureKV &FE = FeatureTable[i];
198
199 if (FeatureEntry->Value == FE.Value) continue;
200
201 if (FeatureEntry->Implies & FE.Value) {
202 Bits |= FE.Value;
203 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
204 }
205 }
206}
207
208/// ClearImpliedBits - For each feature that (transitively) implies this
209/// feature, clear it.
210///
211static
Evan Chengb6a63882011-04-15 19:35:46 +0000212void ClearImpliedBits(uint64_t &Bits, const SubtargetFeatureKV *FeatureEntry,
Bill Wendling4222d802007-05-04 20:38:40 +0000213 const SubtargetFeatureKV *FeatureTable,
214 size_t FeatureTableSize) {
215 for (size_t i = 0; i < FeatureTableSize; ++i) {
216 const SubtargetFeatureKV &FE = FeatureTable[i];
217
218 if (FeatureEntry->Value == FE.Value) continue;
219
220 if (FE.Implies & FeatureEntry->Value) {
221 Bits &= ~FE.Value;
222 ClearImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
223 }
224 }
225}
Jim Laskey34bd5d52005-10-25 15:15:28 +0000226
Evan Cheng276365d2011-06-30 01:53:36 +0000227/// getFeatureBits - Get feature bits a CPU.
Jim Laskey34bd5d52005-10-25 15:15:28 +0000228///
Evan Chenge1bff382011-07-01 00:23:10 +0000229uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU,
Evan Cheng276365d2011-06-30 01:53:36 +0000230 const SubtargetFeatureKV *CPUTable,
231 size_t CPUTableSize,
232 const SubtargetFeatureKV *FeatureTable,
233 size_t FeatureTableSize) {
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000234 if (!FeatureTableSize || !CPUTableSize)
235 return 0;
236
Jim Laskeyb3302db2005-09-01 21:36:18 +0000237#ifndef NDEBUG
238 for (size_t i = 1; i < CPUTableSize; i++) {
239 assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 &&
240 "CPU table is not sorted");
241 }
242 for (size_t i = 1; i < FeatureTableSize; i++) {
243 assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 &&
244 "CPU features table is not sorted");
245 }
246#endif
Evan Chengb6a63882011-04-15 19:35:46 +0000247 uint64_t Bits = 0; // Resulting bits
Chris Lattner3e808a42005-10-23 22:23:13 +0000248
Jim Laskey34bd5d52005-10-25 15:15:28 +0000249 // Check if help is needed
Evan Cheng276365d2011-06-30 01:53:36 +0000250 if (CPU == "help")
Chris Lattner3e808a42005-10-23 22:23:13 +0000251 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
252
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000253 // Find CPU entry if CPU name is specified.
254 if (!CPU.empty()) {
255 const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable, CPUTableSize);
256 // If there is a match
257 if (CPUEntry) {
258 // Set base feature bits
259 Bits = CPUEntry->Value;
Bill Wendling1a636de2007-06-27 23:34:06 +0000260
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000261 // Set the feature implied by this CPU feature, if any.
262 for (size_t i = 0; i < FeatureTableSize; ++i) {
263 const SubtargetFeatureKV &FE = FeatureTable[i];
264 if (CPUEntry->Value & FE.Value)
265 SetImpliedBits(Bits, &FE, FeatureTable, FeatureTableSize);
266 }
267 } else {
268 errs() << "'" << CPU
269 << "' is not a recognized processor for this target"
270 << " (ignoring processor)\n";
Bill Wendling1a636de2007-06-27 23:34:06 +0000271 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000272 }
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000273
Jim Laskeyb3302db2005-09-01 21:36:18 +0000274 // Iterate through each feature
Evan Cheng276365d2011-06-30 01:53:36 +0000275 for (size_t i = 0, E = Features.size(); i < E; i++) {
Evan Chenge1bff382011-07-01 00:23:10 +0000276 const StringRef Feature = Features[i];
Chris Lattner3e808a42005-10-23 22:23:13 +0000277
Jim Laskey839615a2005-09-02 19:27:43 +0000278 // Check for help
Chris Lattner3e808a42005-10-23 22:23:13 +0000279 if (Feature == "+help")
280 Help(CPUTable, CPUTableSize, FeatureTable, FeatureTableSize);
281
Jim Laskeyb3302db2005-09-01 21:36:18 +0000282 // Find feature in table.
283 const SubtargetFeatureKV *FeatureEntry =
284 Find(StripFlag(Feature), FeatureTable, FeatureTableSize);
285 // If there is a match
286 if (FeatureEntry) {
287 // Enable/disable feature in bits
Bill Wendling4222d802007-05-04 20:38:40 +0000288 if (isEnabled(Feature)) {
289 Bits |= FeatureEntry->Value;
290
291 // For each feature that this implies, set it.
292 SetImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
293 } else {
294 Bits &= ~FeatureEntry->Value;
295
296 // For each feature that implies this, clear it.
297 ClearImpliedBits(Bits, FeatureEntry, FeatureTable, FeatureTableSize);
298 }
Jim Laskeyb3302db2005-09-01 21:36:18 +0000299 } else {
Chris Lattnere0c86af2009-08-23 21:41:43 +0000300 errs() << "'" << Feature
301 << "' is not a recognized feature for this target"
302 << " (ignoring feature)\n";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000303 }
304 }
Bill Wendling4222d802007-05-04 20:38:40 +0000305
Jim Laskeyb3302db2005-09-01 21:36:18 +0000306 return Bits;
307}
308
Evan Cheng276365d2011-06-30 01:53:36 +0000309/// Get scheduling itinerary of a CPU.
Evan Chenge1bff382011-07-01 00:23:10 +0000310void *SubtargetFeatures::getItinerary(const StringRef CPU,
Evan Cheng276365d2011-06-30 01:53:36 +0000311 const SubtargetInfoKV *Table,
312 size_t TableSize) {
Jim Laskey34bd5d52005-10-25 15:15:28 +0000313 assert(Table && "missing table");
314#ifndef NDEBUG
315 for (size_t i = 1; i < TableSize; i++) {
316 assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted");
317 }
318#endif
319
320 // Find entry
Evan Cheng276365d2011-06-30 01:53:36 +0000321 const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize);
Jim Laskey34bd5d52005-10-25 15:15:28 +0000322
323 if (Entry) {
324 return Entry->Value;
325 } else {
Evan Cheng276365d2011-06-30 01:53:36 +0000326 errs() << "'" << CPU
Chris Lattnere0c86af2009-08-23 21:41:43 +0000327 << "' is not a recognized processor for this target"
328 << " (ignoring processor)\n";
Jim Laskey34bd5d52005-10-25 15:15:28 +0000329 return NULL;
330 }
331}
332
333/// print - Print feature string.
334///
Chris Lattnere0c86af2009-08-23 21:41:43 +0000335void SubtargetFeatures::print(raw_ostream &OS) const {
336 for (size_t i = 0, e = Features.size(); i != e; ++i)
Jim Laskeyb3302db2005-09-01 21:36:18 +0000337 OS << Features[i] << " ";
Jim Laskeyb3302db2005-09-01 21:36:18 +0000338 OS << "\n";
339}
340
Jim Laskey34bd5d52005-10-25 15:15:28 +0000341/// dump - Dump feature info.
342///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000343void SubtargetFeatures::dump() const {
David Greene9759c302010-01-05 01:29:36 +0000344 print(dbgs());
Jim Laskeyb3302db2005-09-01 21:36:18 +0000345}
Viktor Kutuzove823db82009-11-18 20:20:05 +0000346
Bill Wendling81043ee2010-05-11 00:30:02 +0000347/// getDefaultSubtargetFeatures - Return a string listing the features
348/// associated with the target triple.
Viktor Kutuzove823db82009-11-18 20:20:05 +0000349///
350/// FIXME: This is an inelegant way of specifying the features of a
351/// subtarget. It would be better if we could encode this information
352/// into the IR. See <rdar://5972456>.
353///
Evan Cheng276365d2011-06-30 01:53:36 +0000354void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
Bill Wendlingf6d84812010-05-11 20:46:04 +0000355 if (Triple.getVendor() == Triple::Apple) {
356 if (Triple.getArch() == Triple::ppc) {
357 // powerpc-apple-*
358 AddFeature("altivec");
359 } else if (Triple.getArch() == Triple::ppc64) {
360 // powerpc64-apple-*
361 AddFeature("64bit");
362 AddFeature("altivec");
Viktor Kutuzove823db82009-11-18 20:20:05 +0000363 }
Bill Wendling81043ee2010-05-11 00:30:02 +0000364 }
Viktor Kutuzove823db82009-11-18 20:20:05 +0000365}