LLVM  4.0.0
SubtargetFeature.cpp
Go to the documentation of this file.
1 //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SubtargetFeature interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/Format.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <cctype>
23 #include <cstdlib>
24 using namespace llvm;
25 
26 //===----------------------------------------------------------------------===//
27 // Static Helper Functions
28 //===----------------------------------------------------------------------===//
29 
30 /// hasFlag - Determine if a feature has a flag; '+' or '-'
31 ///
32 static inline bool hasFlag(StringRef Feature) {
33  assert(!Feature.empty() && "Empty string");
34  // Get first character
35  char Ch = Feature[0];
36  // Check if first character is '+' or '-' flag
37  return Ch == '+' || Ch =='-';
38 }
39 
40 /// StripFlag - Return string stripped of flag.
41 ///
42 static inline std::string StripFlag(StringRef Feature) {
43  return hasFlag(Feature) ? Feature.substr(1) : Feature;
44 }
45 
46 /// isEnabled - Return true if enable flag; '+'.
47 ///
48 static inline bool isEnabled(StringRef Feature) {
49  assert(!Feature.empty() && "Empty string");
50  // Get first character
51  char Ch = Feature[0];
52  // Check if first character is '+' for enabled
53  return Ch == '+';
54 }
55 
56 /// Split - Splits a string of comma separated items in to a vector of strings.
57 ///
58 static void Split(std::vector<std::string> &V, StringRef S) {
60  S.split(Tmp, ',', -1, false /* KeepEmpty */);
61  V.assign(Tmp.begin(), Tmp.end());
62 }
63 
64 /// Adding features.
66  // Don't add empty features.
67  if (!String.empty())
68  // Convert to lowercase, prepend flag if we don't already have a flag.
69  Features.push_back(hasFlag(String) ? String.lower()
70  : (Enable ? "+" : "-") + String.lower());
71 }
72 
73 /// Find KV in array using binary search.
76  // Binary search the array
77  auto F = std::lower_bound(A.begin(), A.end(), S);
78  // If not found then return NULL
79  if (F == A.end() || StringRef(F->Key) != S) return nullptr;
80  // Return the found array item
81  return F;
82 }
83 
84 /// getLongestEntryLength - Return the length of the longest entry in the table.
85 ///
87  size_t MaxLen = 0;
88  for (auto &I : Table)
89  MaxLen = std::max(MaxLen, std::strlen(I.Key));
90  return MaxLen;
91 }
92 
93 /// Display help for feature choices.
94 ///
95 static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
96  ArrayRef<SubtargetFeatureKV> FeatTable) {
97  // Determine the length of the longest CPU and Feature entries.
98  unsigned MaxCPULen = getLongestEntryLength(CPUTable);
99  unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
100 
101  // Print the CPU table.
102  errs() << "Available CPUs for this target:\n\n";
103  for (auto &CPU : CPUTable)
104  errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
105  errs() << '\n';
106 
107  // Print the Feature table.
108  errs() << "Available features for this target:\n\n";
109  for (auto &Feature : FeatTable)
110  errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
111  errs() << '\n';
112 
113  errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
114  "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // SubtargetFeatures Implementation
119 //===----------------------------------------------------------------------===//
120 
122  // Break up string into separate features
123  Split(Features, Initial);
124 }
125 
126 
127 std::string SubtargetFeatures::getString() const {
128  return join(Features.begin(), Features.end(), ",");
129 }
130 
131 /// SetImpliedBits - For each feature that is (transitively) implied by this
132 /// feature, set it.
133 ///
134 static
136  ArrayRef<SubtargetFeatureKV> FeatureTable) {
137  for (auto &FE : FeatureTable) {
138  if (FeatureEntry->Value == FE.Value) continue;
139 
140  if ((FeatureEntry->Implies & FE.Value).any()) {
141  Bits |= FE.Value;
142  SetImpliedBits(Bits, &FE, FeatureTable);
143  }
144  }
145 }
146 
147 /// ClearImpliedBits - For each feature that (transitively) implies this
148 /// feature, clear it.
149 ///
150 static
152  const SubtargetFeatureKV *FeatureEntry,
153  ArrayRef<SubtargetFeatureKV> FeatureTable) {
154  for (auto &FE : FeatureTable) {
155  if (FeatureEntry->Value == FE.Value) continue;
156 
157  if ((FE.Implies & FeatureEntry->Value).any()) {
158  Bits &= ~FE.Value;
159  ClearImpliedBits(Bits, &FE, FeatureTable);
160  }
161  }
162 }
163 
164 /// ToggleFeature - Toggle a feature and update the feature bits.
165 void
167  ArrayRef<SubtargetFeatureKV> FeatureTable) {
168 
169  // Find feature in table.
170  const SubtargetFeatureKV *FeatureEntry =
171  Find(StripFlag(Feature), FeatureTable);
172  // If there is a match
173  if (FeatureEntry) {
174  if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
175  Bits &= ~FeatureEntry->Value;
176  // For each feature that implies this, clear it.
177  ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
178  } else {
179  Bits |= FeatureEntry->Value;
180 
181  // For each feature that this implies, set it.
182  SetImpliedBits(Bits, FeatureEntry, FeatureTable);
183  }
184  } else {
185  errs() << "'" << Feature
186  << "' is not a recognized feature for this target"
187  << " (ignoring feature)\n";
188  }
189 }
190 
192  ArrayRef<SubtargetFeatureKV> FeatureTable) {
193 
194  assert(hasFlag(Feature));
195 
196  // Find feature in table.
197  const SubtargetFeatureKV *FeatureEntry =
198  Find(StripFlag(Feature), FeatureTable);
199  // If there is a match
200  if (FeatureEntry) {
201  // Enable/disable feature in bits
202  if (isEnabled(Feature)) {
203  Bits |= FeatureEntry->Value;
204 
205  // For each feature that this implies, set it.
206  SetImpliedBits(Bits, FeatureEntry, FeatureTable);
207  } else {
208  Bits &= ~FeatureEntry->Value;
209 
210  // For each feature that implies this, clear it.
211  ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
212  }
213  } else {
214  errs() << "'" << Feature
215  << "' is not a recognized feature for this target"
216  << " (ignoring feature)\n";
217  }
218 }
219 
220 
221 /// getFeatureBits - Get feature bits a CPU.
222 ///
226  ArrayRef<SubtargetFeatureKV> FeatureTable) {
227 
228  if (CPUTable.empty() || FeatureTable.empty())
229  return FeatureBitset();
230 
231 #ifndef NDEBUG
232  assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
233  "CPU table is not sorted");
234  assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
235  "CPU features table is not sorted");
236 #endif
237  // Resulting bits
239 
240  // Check if help is needed
241  if (CPU == "help")
242  Help(CPUTable, FeatureTable);
243 
244  // Find CPU entry if CPU name is specified.
245  else if (!CPU.empty()) {
246  const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
247 
248  // If there is a match
249  if (CPUEntry) {
250  // Set base feature bits
251  Bits = CPUEntry->Value;
252 
253  // Set the feature implied by this CPU feature, if any.
254  for (auto &FE : FeatureTable) {
255  if ((CPUEntry->Value & FE.Value).any())
256  SetImpliedBits(Bits, &FE, FeatureTable);
257  }
258  } else {
259  errs() << "'" << CPU
260  << "' is not a recognized processor for this target"
261  << " (ignoring processor)\n";
262  }
263  }
264 
265  // Iterate through each feature
266  for (auto &Feature : Features) {
267  // Check for help
268  if (Feature == "+help")
269  Help(CPUTable, FeatureTable);
270 
271  ApplyFeatureFlag(Bits, Feature, FeatureTable);
272  }
273 
274  return Bits;
275 }
276 
277 /// print - Print feature string.
278 ///
280  for (auto &F : Features)
281  OS << F << " ";
282  OS << "\n";
283 }
284 
285 /// dump - Dump feature info.
286 ///
288  print(dbgs());
289 }
290 
291 /// Adds the default features for the specified target triple.
292 ///
293 /// FIXME: This is an inelegant way of specifying the features of a
294 /// subtarget. It would be better if we could encode this information
295 /// into the IR. See <rdar://5972456>.
296 ///
298  if (Triple.getVendor() == Triple::Apple) {
299  if (Triple.getArch() == Triple::ppc) {
300  // powerpc-apple-*
301  AddFeature("altivec");
302  } else if (Triple.getArch() == Triple::ppc64) {
303  // powerpc64-apple-*
304  AddFeature("64bit");
305  AddFeature("altivec");
306  }
307  }
308 }
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:241
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static void Help(ArrayRef< SubtargetFeatureKV > CPUTable, ArrayRef< SubtargetFeatureKV > FeatTable)
Display help for feature choices.
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
static size_t getLongestEntryLength(ArrayRef< SubtargetFeatureKV > Table)
getLongestEntryLength - Return the length of the longest entry in the table.
iterator end() const
Definition: ArrayRef.h:130
static std::string StripFlag(StringRef Feature)
StripFlag - Return string stripped of flag.
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:233
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
Definition: StringExtras.h:232
void AddFeature(StringRef String, bool Enable=true)
Adding Features.
#define F(x, y, z)
Definition: MD5.cpp:51
SubtargetFeatureKV - Used to provide key value pairs for feature and CPU bit flags.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:270
static bool hasFlag(StringRef Feature)
hasFlag - Determine if a feature has a flag; '+' or '-'
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
static const SubtargetFeatureKV * Find(StringRef S, ArrayRef< SubtargetFeatureKV > A)
Find KV in array using binary search.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:587
iterator begin() const
Definition: ArrayRef.h:129
void dump() const
dump - Dump feature info.
static void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry, ArrayRef< SubtargetFeatureKV > FeatureTable)
SetImpliedBits - For each feature that is (transitively) implied by this feature, set it...
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:136
std::string getString() const
Features string accessors.
static void ToggleFeature(FeatureBitset &Bits, StringRef String, ArrayRef< SubtargetFeatureKV > FeatureTable)
ToggleFeature - Toggle a feature and update the feature bits.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static void ClearImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry, ArrayRef< SubtargetFeatureKV > FeatureTable)
ClearImpliedBits - For each feature that (transitively) implies this feature, clear it...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
SubtargetFeatures(StringRef Initial="")
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:716
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
#define I(x, y, z)
Definition: MD5.cpp:54
static bool isEnabled(StringRef Feature)
isEnabled - Return true if enable flag; '+'.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature, ArrayRef< SubtargetFeatureKV > FeatureTable)
Apply the feature flag and update the feature bits.
void print(raw_ostream &OS) const
Print feature string.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
VendorType getVendor() const
getVendor - Get the parsed vendor type of this triple.
Definition: Triple.h:276
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
FeatureBitset getFeatureBits(StringRef CPU, ArrayRef< SubtargetFeatureKV > CPUTable, ArrayRef< SubtargetFeatureKV > FeatureTable)
Get feature bits of a CPU.
static void Split(std::vector< std::string > &V, StringRef S)
Split - Splits a string of comma separated items in to a vector of strings.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
LLVM_NODISCARD std::string lower() const
Definition: StringRef.cpp:122